I am trying to figure out how C and C++ store large objects on the stack. Usually, the stack is the size of an integer, so I don’t understand how larger objects are stored there. Do they simply take up multiple stack ‘slots’?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The stack is a piece of memory. The stack pointer points to the top. Values can be pushed on the stack and popped to retrieve them.
For example if we have a function which is called with two parameters (1 byte sized and the other 2 byte sized; just assume we have an 8-bit PC).
Both are pushed on the stack this moves the stack pointer up:
Now the function is called and the return addres is put on the stack:
OK, within the function we have 2 local variables; one of 2 bytes and one of 4. For these a position is reserved on the stack, but first we save the stack pointer so we know where the variables start by counting up and the parameters are found by counting down.
As you see, you can put anything on the stack as long as you have space left. And else you will get the phenomena that gives this site its name.