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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With