Understanding the stack

Let's take a look at how memory is allocated at runtime. The stack is a block of memory that is associated with a particular process or thread. When we say stack, just think of a stack of dishes. It's orderly, you start at the bottom (the table or counter), and you place a plate on top of the one below it. To get to a plate in the middle of the stack, you need to remove the plates above it first. (Okay, maybe I'm getting a little carried away with this analogy. I used to wait tables.)

This stack organization is called a Last in, First out (LIFO) structure. Getting data on the stack is called a push. Getting data off the stack is one of my favorite terms in computing: pop. Sometimes you'll see pullbut let's be honest, pop is much more fun. During the execution of a program, when a function is called, the function and its data are pushed onto the stack. The stack pointer keeps an eye on the top of the stack as data is pushed and popped off the stack. Finally, after all the data in the procedure has been popped off the stack, the final piece of information is a return instruction that takes us back to where we were in the program before the call began. Since the program data is in memory, the return is an instruction to jump to a particular memory address.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.161.222