What is the maximum stack size when using the standard C ++ library stack class?

What is the maximum stack size when using the C ++ Standard Library stack

? Or can you determine its maximum size? I searched but could not find an answer.

+3


source to share


4 answers


The stack is a container adapter, so its limit depends on the limit of the underlying container. By default it is deque

By default, if no container class is specified for a specific instance of a stack class, the standard container pointer is used.

Due to system or library implementation constraints, this can be found using the max_size function :

// deque::max_size
#include <iostream>
#include <deque>

int main ()
{
  unsigned int i;
  std::deque<int> mydeque;

  std::cout << mydeque.max_size(); // 1073741823

  return 0;
}

      



It returns 1073741823 in a linked program as an example.

You should also keep in mind that:

This is the maximum potential size that a container can reach due to known limitations on the system or library implementation, but the container is by no means guaranteed to be able to reach this size: it may still not allocate memory at any one before reaching this size.

i.e. these are theoretical design limits, you should not approach these limits in a typical use case. Similar considerations apply to other containers as well.

+4


source


None exist. The main container will try to get more memory from the heap (and eventually the OS) if it runs out. If that fails, you usually get an exception std::bad_alloc

, although some systems are broken in this regard and need special configuration to work.



+2


source


You have two limits on the number of objects stored in a container:

  • The maximum value that the capacitance variable can hold.
  • Memory is available for your program.

Container Capacity
Consider this on a very small container that uses the uint8_t variable to count the number of items in a counter.

A uint8_t

variable can hold 256 variables, so the container will be limited to 255 elements, regardless of the amount of memory available to your system.

Memory Limits
Each platform has a limited amount of memory.

On many platforms, the operating system is responsible for allocating memory to your program. For example, your platform might be running an application that uses a lot of memory and so there isn't much left for your program.

If a container uses an unsigned 64-bit integer for its throughput and indexing, the container's capacity may still be limited by the memory allocated to your program. If your program has 1024 bytes of memory allocated, then the capacity of your container is no more than 1024 / object_size. So if my object is 256 bytes, the maximum amount I can store in my container would be 1024/256 or 4 objects.

Summary The
capacity of a container is limited by the range that their indexing or capacity variable uses, the size of the objects in the container, and the amount of memory allocated to your program. All containers will have some overhead that would reduce the container's capacity (eg containers based on linked lists would need extra memory for the reference fields). Sometimes the memory allocated for your program can be the amount left over after loading your program. Usually, container capacity is not a concern unless the program runs on a memory-constrained system, such as most embedded platforms without hard drives.

+1


source


"What is the maximum stack size when using the standard C ++ library stack class?"

I'm afraid this is implementation / OS dependent. In theory, this should target what std::stack::size_type

can be held.

0


source







All Articles