stack memory

Hello I am trying to learn about stack memory, and have come across a page that I am struggling slightly with that I was hoping someone might be able to clear up for me. The page says

"Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function."

But what I am struggling to understand is this part

"Each time a function is called, the address of where to return to"

Does this mean, that it is storing the address of say the previous function or variable so that when that function that was called is finished, the stack pointer can move back down. Or does it mean it is storing the address of where it has to return that called function to?
A compiled program is made of a series of instructions. While the program is running, those instructions are stored in memory and each resides at a specific location. If you disassemble a program you might encounter something like
DEADBEEF | push 42
DEADBEF4 | call foo
DEADBEF9 | cmp eax
The numbers on the left are the memory positions of the instructions. When you call foo you want the CPU to continue executing the rest of the caller function once foo returns, so the current value of the program counter (the CPU register that stores the address of the current instruction) needs to be saved before jumping to foo. So call foo pushes onto the stack the value DEADBEF9. When foo executes a ret instruction, the CPU will pop from the stack DEADBEF9 and will resume executing the caller
Here is an online tool that will help you visualize what is going on with your code in asm:

An on disk decompiler will also show you the registers on your CPU. I learned with Ollydbg, but unfortunately has not kept up with the times and I'm afraid I don't have a modern suggestion.
the cpu has a concept called the instruction pointer, which is 'where the next executable line is' in the machine language code. When you call a function, the current value (it is a memory address) is stored so when the function ends, it can resume where it was. There is a lot going on inside to accomplish this, but the idea is easy to see from this simple description, I hope.
Note that various hardware and or operating systems may do this slightly differently down in the details.
Last edited on
Hello, thank you computergeek for the link, I tried it out and I think it was a bit advanced for me just now, but will hold on to the link and hopefully be able to understand it all soon enough.

I think I understand what it is saying now, and the concept of the instruction pointer, please correct me if i am wrong (I might get a bit confused at the end of my explanation when the stack comes into play). In simple terms : All instructions have their own memory address, and the instruction pointer will hold the address of the next instruction to run.

So by that paragraph saying "the address of where to return to" its basically just the next instruction to run, ie - back to the main function (main function being ran), and that memory address at this point will have been stored on the stack (according to that paragraph?) and so the instruction pointer would have read it from the stack?
All instructions have their own memory address

More or less. There are what are called implicit instructions that are operations built into the architecture of the processor. These are things like increment, decrement etc which don't have addresses per say.

the instruction pointer will hold the address of the next instruction to run.

Yes, the instruction pointer is a register on the processor. What gets stored on the stack in C++ is the RAII stuff, terminators and the like.
Last edited on
Topic archived. No new replies allowed.