2

I am trying to get the concept of stack and i have confusion i tried to find answer but couldn't find.

Ok so values store in stack only from top it means it grows as values position grows one by one and it means it the direction of growth should be upward as shown in the image :

Stack grows upward example

But Stack grows downward as described. How??

if i am understanding correct then:

when values store in stack it take spaces so it means stack is not growing because its memory is decreasing , But when values return from the stack then the stack grows because storage are being free in stack. and also one of answer on stackoverflow says Direction of glow of stack is opposite to the direction of glow Am i getting right ??? if not please explain

Community
  • 1
  • 1
  • The memory available to the stack is decreasing... that doesn't mean that the memory occupied by the stack is decreasing as more things are added to the stack. – ArtOfWarfare Nov 07 '16 at 14:34
  • @ArtOfWarfare then why and how it says stack grows downward ? –  Nov 07 '16 at 14:35
  • Write "10" at the bottom of the red arrow and write "0" above. Now you have a stack that grows down. – Art Nov 07 '16 at 15:22
  • Alternatively: Turn the image upside down. Now you have a stack that grows down. Turn the image 90 degrees and you now have a stack that grows to the side. Or maybe we should just consider a picture to be a picture. – Art Nov 07 '16 at 15:25

2 Answers2

5

A stack usually means any last-in first-out (LIFO) data structure. It's a data structure which has the operations "push" and "pop". Whether the memory locations of items on the stack "grow" downwards or upwards is an implementation detail and it's possible to implement it either way.

When programmers say the stack we usually mean the call stack of the current process (thread). On my machine, the call stack does grow downwards so that after a "push" instruction, the top of the stack is at a lower address. This is also an implementation detail and isn't something that a C programmer usually has to worry about.

jforberg
  • 6,537
  • 3
  • 29
  • 47
  • But call stack means when we call the function or something and it means stack is returning values now so returning values means "pop" and you said "push" ? –  Nov 07 '16 at 14:44
  • I'm talking about the machine instruction "push" here. When you "push", for example because you enter a new function, you add a value to the stack and therefore the top of the stack will move to a lower address. This is what we mean when we say the stack "grows downwards". – jforberg Nov 07 '16 at 14:46
3

Historically, the "bottom" of the stack is at the top of the memory, growing downward, and the heap started at the bottom of the memory, growing upward. This gives the smallest chance of them overwriting each other.

From this history, e.g. on Intel the push and pop CPU instructions work with this down growing stack. So if you push something onto the stack, the stack pointer decreases and if you pop something it increases.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41