stack

S

Salt_Peter

I was just wondering why the stack seems to grow downwards on x86 C++
code.

A stack is a stack, doesn't matter if its x86, 8088, Z80, Forth, C, C+
+, or any other platform/language.

Its a historical relic which has sound logic to it (don't fix
something that's not broken). Way back when you use to have 2 stacks,
one that stored the program's operations and one that stored its data.
The former grew upwards, the latter downwards. If either stack got big
enough to step on the other's shoes, at least you weren't wrapping
around the memory area (register SP stack pointer overflow). The last
thing you wanted then was overwriting critical areas of your program
or loosing the top of a stack (which often meant an overwite of
critical stack pointers, ouch), sometimes causing great hardship to
more than just the program (wow, the stack pointer is now pointing
where?).

You can also look at it from another point of view, where would you
place an upward growing stack in order to provide it with enough space
to safely grow without colliding with some other allocation or part?
Placing the stack at the top and growing it down is the natural
solution. Basicly: it protects the stack's internal pointers from
being overwritten by something growing upwards like a buffer overflow.
 
J

Jack Klein

I was just wondering why the stack seems to grow downwards on x86 C++
code.

Then ask in a group that supports the particular x86 C++ compiler that
you are using. The language does not define or require a stack, let
along say anything at all about its direction. So this is an
implementation-specific issue, not a language one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top