Understanding stack frame of function call in C/C++? [closed]

Your question is borderline here. programmers could be a better place.

A good book to understand the concepts of stack etc might be Queinnec’s Lisp In Small Pieces (it explains quite well what a stack is for Lisp). Also, SICP
is a good book to read.

D.Knuth‘s books and MMIX is also a good read.

Read carefully Wikipedia Call stack page.

In theory, no call stack is needed, and some languages and implementations (e.g. old SML/NJ) did not use any stack (but allocated the call frame in the garbage collected heap). See A.Appel’s old paper Garbage Collection Can be Faster than Stack Allocation (and learn more about garbage collection in general).

Usually C and C++ implementations have a stack (and often use the hardware stack). Some C local variables might not have any stack location (because they have been optimized, or are kept in a register). Sometimes, the stack location of a C local variable may change (the compiler would use one call stack slot for some occurrences, and another call stack slot for other occurrences of the same local variable). And of course some temporary values may be compiled like your local variables (so stay in a register, on in one stack slot then another one, etc….). When optimizing the compiler could do weird tricks with variables.

On some old machines IBM/360 or IBM z/series, there is no hardware stack; the stack used by the C compiler is a software convention (e.g. some register is dedicated to that usage, without specific hardware support)

Think about the execution (or interpretation) of a recursively defined function (like the good old factorial naively coded). Read about recursion (in general, in computer science), primitive recursive functions, lambda calculus, denotational semantics, stack automaton, register allocation, tail calls, continuations, ABI, interrupts, Posix signals, sigaltstack(2), getcontext(2), longjmp(3)etc…. etc…

Read also books about Computer Architecture. In practice, the call stack is so important that several hardware resources (including the stack pointer register, often the call frame base pointer register, and perhaps hidden machinery e.g. cache related) are dedicated to it on common processors.

You could also look at the intermediate representations used by the GCC compiler. Then use -fdump-tree-all or the GCC MELT probe. If looking at the generated assembly be sure to pass -S -fverbose-asm to your gcc command.

See also the linux assembly howto.

I gave a lot of links. It is difficult to answer better, because I have no idea of your background.

Leave a Comment