[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

should program call stack grow upward or downwards?

Tammy

5/24/2011 9:24:00 PM

Hi there:

I know this is a very fundamental question. I am still quite confused
if the program call stack stack should always grow upwards from the
bottom, or the opposite, or doesn't matter??

That means the stack pointer should go upwards when there are "push"
operations,
and stack pointer should go downards when there are "pop" operations??

If this is the case, the address should go upwards (increasing) or
downards (decreasing) then? i.e. The top of the stack should have
address 00000000, or FFFFFFFF? How do we decide that?

Thanks!
19 Answers

Ian Collins

5/24/2011 9:28:00 PM

0

On 05/25/11 09:23 AM, Simon wrote:
> Hi there:
>
> I know this is a very fundamental question. I am still quite confused
> if the program call stack stack should always grow upwards from the
> bottom, or the opposite, or doesn't matter??

It doesn't matter. It is a typically a hardware property.

--
Ian Collins

Tammy

5/24/2011 9:29:00 PM

0

Ian Collins writes:
> On 05/25/11 09:23 AM, Simon wrote:
>> Hi there:
>>
>> I know this is a very fundamental question. I am still quite confused
>> if the program call stack stack should always grow upwards from the
>> bottom, or the opposite, or doesn't matter??
>
> It doesn't matter. It is a typically a hardware property.

I see. Isn't it then important to avoid writing code that depends on
stack growth direction?

Shao Miller

5/24/2011 9:34:00 PM

0

On 5/24/2011 17:28, Simon wrote:
> Ian Collins writes:
>> On 05/25/11 09:23 AM, Simon wrote:
>>> Hi there:
>>>
>>> I know this is a very fundamental question. I am still quite confused
>>> if the program call stack stack should always grow upwards from the
>>> bottom, or the opposite, or doesn't matter??
>>
>> It doesn't matter. It is a typically a hardware property.
>
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

Were you going to write this code in C, assembly language, or something
else? Are you concerned about portable code at all, or are you only
concerned with a particular architecture?

China Blue Veins

5/24/2011 9:36:00 PM

0

In article <irh7kn$etq$1@speranza.aioe.org>, Simon <anon@nospam.com> wrote:

> Hi there:
>
> I know this is a very fundamental question. I am still quite confused
> if the program call stack stack should always grow upwards from the
> bottom, or the opposite, or doesn't matter??

It doesn't matter. Some machines go down, others go up.

Before VM was widespread, it was customary to put code, statics, and then the
heap growing up from low memory, and the stack at high memory growing down. If
the two collided, the program would die. Sometimes horribly. With VM it really
doesn't matter since virtual addresses have little relation to real addresses.

> That means the stack pointer should go upwards when there are "push"
> operations,
> and stack pointer should go downards when there are "pop" operations??

This varies. Also varying is whether the SP is the first available stack space
or the latest allocated space. So all of these occur:
push pop
*++sp = value value = *sp--
*sp++ = value value = *--sp
*--sp = value value = *sp++
*sp-- = value value = *++sp

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

China Blue Veins

5/24/2011 9:45:00 PM

0

In article <irh7ur$fij$1@speranza.aioe.org>, Simon <anon@nospam.com> wrote:

> Ian Collins writes:
> > On 05/25/11 09:23 AM, Simon wrote:
> >> Hi there:
> >>
> >> I know this is a very fundamental question. I am still quite confused
> >> if the program call stack stack should always grow upwards from the
> >> bottom, or the opposite, or doesn't matter??
> >
> > It doesn't matter. It is a typically a hardware property.
>
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

Array elements are stored at increasing address within the space allocated for
that array. You should not assume any relation between the addresses of
different variables or addresses returned by malloc.

int q, r[2];

We know that &r[0]<&r[1] and (&q<&r[0] or &q>&r[1]), but we don't know if
&q<&r[0] or &q>&r[1].

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

Keith Thompson

5/24/2011 9:52:00 PM

0

Simon <anon@nospam.com> writes:
> Ian Collins writes:
>> On 05/25/11 09:23 AM, Simon wrote:
>>> I know this is a very fundamental question. I am still quite confused
>>> if the program call stack stack should always grow upwards from the
>>> bottom, or the opposite, or doesn't matter??
>>
>> It doesn't matter. It is a typically a hardware property.
>
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

Yes.

If you can show us an example of C code that depends on the direction
of stack growth, perhaps we can help you fix it. (Frankly, it
wouldn't even occur to me to write such code; 99+% of the time,
knowing which way the stack grows isn't even useful.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ian Collins

5/24/2011 9:53:00 PM

0

On 05/25/11 09:28 AM, Simon wrote:
> Ian Collins writes:
>> On 05/25/11 09:23 AM, Simon wrote:
>>> Hi there:
>>>
>>> I know this is a very fundamental question. I am still quite confused
>>> if the program call stack stack should always grow upwards from the
>>> bottom, or the opposite, or doesn't matter??
>>
>> It doesn't matter. It is a typically a hardware property.
>
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

Yes, if you want it to be portable. You would have to perform
contortions write code that did stack growth direction!

--
Ian Collins

Seebs

5/24/2011 9:58:00 PM

0

On 2011-05-24, Simon <anon@nospam.com> wrote:
> I know this is a very fundamental question. I am still quite confused
> if the program call stack stack should always grow upwards from the
> bottom, or the opposite, or doesn't matter??

Doesn't matter.

> If this is the case, the address should go upwards (increasing) or
> downards (decreasing) then? i.e. The top of the stack should have
> address 00000000, or FFFFFFFF? How do we decide that?

You don't.

Okay, a few things:
1. There are real machines on which there *isn't* a "stack" in the sense
you discuss.
2. Even on machines where there is, it simply doesn't matter. You should
never, ever, be writing code in C which would be affected by this.
3. Even if you were, neither of those addresses would be a likely candidate.

Basically, if you are thinking about "the stack" as some kind of contiguous
region of memory, you are not thinking about C, but about some specific
machine. For C, it's enough to know that automatic variables have a lifetime
which ends when you leave the block in which they are declared.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Seebs

5/24/2011 9:58:00 PM

0

On 2011-05-24, Simon <anon@nospam.com> wrote:
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

Yes, absolutely!

I don't think you can write code which depends on stack growth direction
and isn't awful, in C.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Joe Pfeiffer

5/24/2011 10:13:00 PM

0

Simon <anon@nospam.com> writes:

> Ian Collins writes:
>> On 05/25/11 09:23 AM, Simon wrote:
>>> Hi there:
>>>
>>> I know this is a very fundamental question. I am still quite confused
>>> if the program call stack stack should always grow upwards from the
>>> bottom, or the opposite, or doesn't matter??
>>
>> It doesn't matter. It is a typically a hardware property.
>
> I see. Isn't it then important to avoid writing code that depends on
> stack growth direction?

No, because any code that *does* depend on stack growth direction is
badly broken in other ways as well, and fixing that other brokenness
will also make it stop depending on the direction of stack growth.
--
"It used to be that the USA was pretty good at producing stuff teenaged
boys could lose a finger or two playing with." -- James Nicoll