Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C++, what happens if the size of the array used to store the stock is exceeded by the push operation?
Updated 16-Sep-10 9:12am
Richard MacCutchan 16-Sep-10 7:53am
I would expect your program to raise an exception or to crash.

A "Stack Overflow" will be raised.[^]

When expecting high use of stack you can configure your program to inform the OS to reserve a larger stack. Most languages will give you an option to define the stack size.

Good luck!
AspDotNetDev 16-Sep-10 15:36pm
Notice the OP mentions the push operation. It is likely he/she is referring to the stack class, rather than the call stack.
E.F. Nijboer 17-Sep-10 5:03am
It's not completely clear because the word class isn't mentioned in the question. When passing an array by value this would mean that the complete array would be pushed onto the stack for local use. Not very efficient in the first place, but could mean that this will cause a stack overflow.
AspDotNetDev 19-Sep-10 15:47pm
Note also that the OP says "the array used to store the stack", not "the array stored on the stack" (I corrected the spelling).
An out of memory exception occurs (not a stack overflow, which occurs with the call stack, not the array used to store values in a stack).

To prevent this, you can create a custom implementation of the stack class that uses an array of arrays to store values. That way, memory fragmentation will not prevent the small arrays from being created.
AspDotNetDev 16-Sep-10 15:23pm
I should add that I don't know how the C++ stack is implemented. If it uses a linked list (not likely, as that takes up more memory), then you would not encounter this problem. It is very likely, though, that the underlying data structure is a single array.
markkuk 16-Sep-10 16:34pm
STL stack uses deque by default, but you can use any container template that provides back(), push_back() and pop_back() functions. Deque doesn't require contiguous memory and it may be implemented as an "array of arrays".
AspDotNetDev 16-Sep-10 19:14pm
Thanks for the info. Looks like I barely scratched the surface of C++ back when I was using it 7 years ago. Wow, that's a long time... I should really refresh my C++ skills.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900