Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stack

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.

like image 958
user3255175 Avatar asked Dec 14 '25 06:12

user3255175


1 Answers

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.

like image 107
Marco A. Avatar answered Dec 16 '25 22:12

Marco A.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!