Friday, 28 March 2014

The "Stack"

Since a lot of beginning programmers rarely hear of the 'stack' (if they even hear about it), I thought I'd post a somewhat introductory description of what the stack is.

  What is the stack? The stack is an area of memory that the operating system gives to you when your program starts up. It is used to hold all of your function’s local variables and parameters. Why is it called a stack? In truth, the stack is not “a stack” but just a chunk of memory we use. However, we do ‘conceptually’ think of it as a stack to aid in understanding it. More specifically, we ‘imagine’ the stack as looking like this:

Now, what’s the green arrow? That is the “stack pointer.” Our program uses it to keep track of where the top of the stack is. Anything below the stack pointer is memory that is in use (either for variables, parameters or something); the memory above the stack pointer is completely free. Everytime we add something to the stack, the pointer goes up until it’s right above the top of the newly added data. This way, our programs can keep track of what’s on the stack and where it can start adding new data onto the stack. Also, you’ll hear these words popping up in your programming studies, so here they are:

“Push” – Means to add something onto the top of the stack.
“Pop” – Means to remove something from the top of the stack.

There is no going “in-between” or “from the bottom.” A stack is a stack. You can only add something to the top or remove something from the top.

Here’s a sample program that should help you understand:

int add(int x, int y)
{
return x + y;
}

int main()
{
  int x;
  int y;
  add(x, y);
  return 0;
}

This is what the stack looks like:
Now when the line: int x; gets executed, the computer makes room on the stack by simply moving the stack pointer UP like:

As you can see, the stack pointer is now on top of the local variable ‘x’.
When int y; executes, ‘y’ will get “pushed” onto the stack making the stack look like:


Now, the function call to add() gets executed: add(x, y);
And we pass in x and y BY VALUE. When we say BY VALUE, a copy of x and y will be put onto the stack like this:


As you can see, add()’s parameters: x and y, ARE variables, BUT they are separate variables with their own copies of the data you pass in. When the add() function’s return statement executes, then the stack pointer “goes back down” indicating that the memory above it is now available. Some people also say that the parameters are “popped” off the stack. So, now the stack looks like this:


As you can see, the memory originally used for add()’s parameters are now “deallocated” – made available for use again. What is the stack pointer? It is merely a variable that contains the starting “address” of the available memory on the stack. Remember that each “byte” of memory has an address (just a number) associated with it. The stack pointer contains the address of the first byte of available free space on the stack. All the other bytes “on top” of this one byte of free space, is free too; all the other bytes “below” this byte is being used.

I hope this helps. Remember that this is just a basic introduction. There are lots of other information out there that you can research. If you need to ask a question, email me at:

sparkprogrammer@gmail.com
Joe – C++ tutor

No comments:

Post a Comment