code break said:
What is the difference between stack pointer and frame pointer ?
The C language standard does not know anything about stacks
or frames. Stacks and frames are artifacts of particular implementations
and could differ between implementations.
[Off topic]
A stack pointer usually changes during the execution of any one
function, to hold temporary values or to hold variables whose
lifetime is only the scope of a block. For example,
int foo(void) {
int bar;
int totalbaz;
totalbaz = 0;
for (bar = 0; bar<10; bar++) {
int baz;
baz = 7*bar*bar*bar - 3*bar*bar + 2*bar + 5;
totalbaz += baz;
}
return totalbaz;
}
In this routine, baz only needs to exist within the for() loop,
so storage space for baz does not need to be allocated until the
for() loop starts executing, and the storage space could be released
at the end of the for() loop. baz would be a good candidate for being
allocated on a stack (in an implementation that used stacks.)
(Yes, it would be even better in a register, but the example can
be extended into something too large or complex to hold in registers.)
A frame pointer, on the other hand, usually points to the beginning
of the storage space allocated for any one function, and does not
change during the execution of that function. In the above example,
there could be a frame pointer pointing to a block of storage,
and at that block of storage there might be the address to return
to followed by the storage for totalbaz followed by the storage for bar --
so inside the for() loop, the compiler might refer to bar as being
a certain distance relative to the frame pointer. A frame pointer
usually points to the beginning of the fixed information about an
invocation of a function, The stack pointer -might- start from
the end of the fixed information for the frame, or the stack -might-
be somewhere else completely in memory... that's an implementation
decision. And some implementations don't use frame pointers at all.