TheGameiswar
TheGameiswar

Reputation: 28920

How StackFrames are impacted when GOTO is used?

I am following Memory Layout And The Stack to understand stack frames..

The Author explains stack frames nicely and says what will be in STACK at given point of time

He also asks some questions below

Below is my attempt to answer above questions

  • Suppose a program makes 5 function calls. How many frames should be on the stack?

If the functions are independent of each other,then only main function or main and calling function will be in stack

We saw that the stack grows linearly downward, and that when a function returns, the last frame on the stack is deallocated and returned to unused memory. Is it possible for a frame somewhere in the middle of the stack to be returned to unused memory? If it did, what would that mean about the running program?

I think this is not possible,since this may corrupt memory

I am not sure of rest of the questions.Can you please help on this

let me know if you need more info

Upvotes: 3

Views: 892

Answers (1)

gsamaras
gsamaras

Reputation: 73384

Suppose a program makes 5 function calls. How many frames should be on the stack?

6, at the most (assuming that no function returns before all functions are been called). One for main() and one for every function call.

We saw that the stack grows linearly downward, and that when a function returns, the last frame on the stack is deallocated and returned to unused memory. Is it possible for a frame somewhere in the middle of the stack to be returned to unused memory? If it did, what would that mean about the running program?

No, since a stack is a that does not allow random access deletion. You can only pop from a stack.

Can a goto() statement cause frames in the middle of the stack to be deallocated? The answer is no, but why?

Because it will alter stack unwinding. Exiting a code block using goto will not unwind the stack which is one of the reasons you should (almost) never use goto. Read more in What is stack unwinding? However, notice this is C++ related, in C goto out of block is well-defined, as Antti mentioned.

Can longjmp() cause frames in the middle of the stack to be deallocated?

I think yes, since it's a spacial case. But that a bit contradicting to my 2nd answer.

When a "non-local goto" is executed via setjmp/longjmp, normal "stack unwinding" does not occur. Therefore any required cleanup actions will not occur either.

longjmp() function from C (see The Wikipedia entry for setjmp.h for the basics). longjmp() jumps down the stack to the corresponding setjmp(), across as many stack frames as there are in between. Unlike the other approaches we will see, longjmp() does not iterate over the list of frames, it just blasts through them all at once with wanton disregard for anything precious or beautiful that they might have contained.

Read more about it here.

longjmp() will return the stack to the state previously saved with setjmp(). If any frames in the middle are removed, then so are all frames below; i.e. there will be no "holes" in the stack after a longjmp, as Klas mentioned.

Upvotes: 6

Related Questions