Frame Pointer Vs Stack Pointer
Related Blog Items
- Stack
- Calling conventions on the x86 platform
- What's the difference between "const X* p", "X* const p" and "const X* const p"?
- When to use pointer, when to use reference?
- Reversing a single linked list using stack
Stack look like the below figure, when we are starting in main() and no function is called from
the main() in a C program:

Suppose, inside of body of main() there’s a call to foo(), which takes two arguments. One way to pass the arguments to foo() is through the stack. When the function foo() is called, the function foo() takes some space in stack for its local variables, hence the stack looks like as below:

In the above figure, we notice SP and FP. SP is the stack pointer and FP is the frame pointer. The frame pointer points to the location where the stack pointer was, just before foo() moved the stack pointer for foo()’s own local variables.
Having a frame pointer is convenient when a function is likely to move the stack pointer several times throughout the course of running the function. The idea is to keep the frame pointer fixed for the duration of foo()’s stack frame. The stack pointer, in the meanwhile, can change values.
And, once it’s time to exit foo(), you just have to set the stack pointer to where the frame pointer is, which effectively pops off foo()’s stack frame. It’s quite handy to have a frame pointer.
So when we exit foo() the stack looks just as it did before we pushed on foo()’s stack frame, except this time the return value has been filled in.
Popularity: 14%
You need to log on to convert this article into PDF
Related Blog Items - Stack
- Calling conventions on the x86 platform
- What's the difference between "const X* p", "X* const p" and "const X* const p"?
- When to use pointer, when to use reference?
- Reversing a single linked list using stack
Related Blog Items
- Stack
- Calling conventions on the x86 platform
- What's the difference between "const X* p", "X* const p" and "const X* const p"?
- When to use pointer, when to use reference?
- Reversing a single linked list using stack
[…] Original post by Pratap and software by Elliott Back […]
January 16th, 2007 at 5:14 pm