user3255175
user3255175

Reputation: 67

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

What is the maximum size of a stack when using the standard C++ library stack class? Or can you define its maximum size? I've been searching but have not found the answer.

Upvotes: 3

Views: 264

Answers (4)

Thomas Matthews
Thomas Matthews

Reputation: 57688

You have two limits to the amount of objects stored in a container:

  1. Maximum value the capacity variable can hold.
  2. Memory available to your program.

Container Capacity
Let's consider this on a very small container, one that uses a uint8_t variable to count the number of elements in the counter.

A uint8_t variable can hold 256 variables, so the container would be limited to 255 items, regardless of the amount of memory available to your system.

Memory restrictions
Every platform has a limited amount of memory.

In many platforms, the Operating System is in charge of allocating memory to your program. For example, your platform may be running an application that uses a lot of memory and therefore there is not a lot left over for your program.

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

Summary
Container capacities are 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 capacity of the container (for example, containers based on linked lists would need addition memory for the link fields). Sometimes the memory allocated to your program may be the amount remaining after your program has loaded. Usually, container capacities are not a worry unless the program is running on a memory constrained system, such as most embedded platforms without hard drives.

Upvotes: 1

James Kanze
James Kanze

Reputation: 153909

There isn't one per se. The underlying container will attempt to get more memory from the heap (and ultimately, the OS) if it runs out. If this fails, you should usually get an std::bad_alloc exception, although some systems are broken in this regard, and need special configuration to work.

Upvotes: 2

Marco A.
Marco A.

Reputation: 43662

A stack is a container adapter, its limit is therefore dependent on the limit of the underlying container. By default this is a deque

By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.

Due to system or library implementation limits, this can be found with 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;
}

Example

As an example value, it returns 1073741823 on the linked program.

You should also keep in mind that:

This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached.

i.e. these are theoretical design limits, you're not supposed to approach these limits in a normal usage scenario. Similar considerations hold for other containers.

Upvotes: 4

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"What is the max size of a stack when using the standard C++ library stack class?"

I'm afraid that's implementation/OS dependent. In theory it should aim to what's std::stack::size_type is able to hold.

Upvotes: 0

Related Questions