473,473 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

direction of stack growth

Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?
I know this question is implementation specific and this may not be the
correct place
to ask this. But any hints would help me a lot.

I thought of the following way (assuming that the implementation uses
stack to
save the local variables as for instance in m68k processor).
In a function f1(), declare a local variable (local1).
save its address. Call another function f2() from f1 and in f2()
declare another local
variable (local2) and return its address to f1(). Now, compare the
addresses of local1
and local2 to determine the direction of stack growth.

Is this the right way of doing it ? Can we compare the adresses of the
local variables
local1 and local ? If not, what is the right of doing it ?
Also, some of the implementations don't use stack to store the local
variables. They store
the local varaibles in registers. In those implementations what would
happen if I try
to print address of local variable (using &local1) ? I am asking this,
as the local
variable may not have been allocated on stack or some memory location.
The compiler
might have used some general purpose register for that. In that case,
what output
we will get if we print the address of a local variable ?
Thanx for any help/hint ....

Apr 19 '06 #1
18 7316
ju**********@yahoo.co.in wrote:
Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?


There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie automatically."
Apr 19 '06 #2

Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?


There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably. But, can you suggest some way for
a
specific implementation (as for instance motorola's 68k processor) ?

Apr 19 '06 #3
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:
Hi all,

Is there any way by which we mat determine the direction of
stack growth (from higher address to lower address Or from lower
address to higher address) ? I know this question is implementation
specific and this may not be the correct place to ask this. But any
hints would help me a lot.
There is no portable way to determine this.
I thought of the following way (assuming that the implementation
uses stack to save the local variables as for instance in m68k
processor). In a function f1(), declare a local variable (local1).
save its address. Call another function f2() from f1 and in f2()
declare another local variable (local2) and return its address to
f1(). Now, compare the addresses of local1 and local2 to determine
the direction of stack growth.
Relational operators on pointer values are defined only when both
pointers point within the same object (or just past the end of it).
A comparison like "&local1 < &local2" invokes undefined behavior.

It's likely to work as you expect on most implementations, but it's
not guaranteed by the standard.

Is this the right way of doing it ? Can we compare the adresses of
the local variables local1 and local ? If not, what is the right of
doing it ? Also, some of the implementations don't use stack to
store the local variables. They store the local varaibles in
registers. In those implementations what would happen if I try to
print address of local variable (using &local1) ? I am asking this,
as the local variable may not have been allocated on stack or some
memory location. The compiler might have used some general purpose
register for that. In that case, what output we will get if we print
the address of a local variable ?


You can legally take the address of any object that's not declared
with the "register" keyword. If the implementation chooses to store
the object in a register, it must also store it in memory if you take
its address. (Most likely, taking the address of an object will cause
the compiler *not* to store it in a register.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 19 '06 #4
ju**********@yahoo.co.in wrote:

Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
> Hi all,
>
> Is there any way by which we mat determine the direction of stack
> growth (from
> higher address to lower address Or from lower address to higher
> address) ?


There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably. But, can you suggest some way for
a
specific implementation (as for instance motorola's 68k processor) ?


Not apart from the non-portable method you suggest.

But /why/ do you want to know this?

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie automatically."
Apr 19 '06 #5
Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
Chris Dollin wrote:
ju**********@yahoo.co.in wrote:

Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?
There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?

I know, this cannot be done portably. But, can you suggest some way for
a
specific implementation (as for instance motorola's 68k processor) ?


Not apart from the non-portable method you suggest.

But /why/ do you want to know this?

Are you trying to second guess the way it's done in gcc build? Surely
it worked at some time in the past, if not now. Why not go to a
newsgroup where this is on topic?
Apr 19 '06 #6

Keith Thompson wrote:
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:
Hi all,

Is there any way by which we mat determine the direction of
stack growth (from higher address to lower address Or from lower
address to higher address) ? I know this question is implementation
specific and this may not be the correct place to ask this. But any
hints would help me a lot.


There is no portable way to determine this.
I thought of the following way (assuming that the implementation
uses stack to save the local variables as for instance in m68k
processor). In a function f1(), declare a local variable (local1).
save its address. Call another function f2() from f1 and in f2()
declare another local variable (local2) and return its address to
f1(). Now, compare the addresses of local1 and local2 to determine
the direction of stack growth.


Relational operators on pointer values are defined only when both
pointers point within the same object (or just past the end of it).
A comparison like "&local1 < &local2" invokes undefined behavior.

It's likely to work as you expect on most implementations, but it's
not guaranteed by the standard.


Thanx a lot for your help. I have one more doubt that why it is illegal
to
compare the addresses of two local variables ? One reason I may think
of
is that pointers are not plain integers and they may be composed of
base and offset. But, in that case as well, the base address of the
segment
(stack segment ) should be the same as both the local variables are on
the same stack (although the stack frames are different).
So, why their comparison is illegal ?

Apr 19 '06 #7
ju**********@yahoo.co.in wrote:
Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ? There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably.


Then you know it is not topical here since you've been around here before.
But, can you suggest some way for
a
specific implementation (as for instance motorola's 68k processor) ?


Yes, read the documentation.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 19 '06 #8
In article <11**********************@t31g2000cwb.googlegroups .com>,
ju**********@yahoo.co.in <ju**********@yahoo.co.in> wrote:
Thanx a lot for your help. I have one more doubt that why it is
illegal to compare the addresses of two local variables ? One reason
I may think of is that pointers are not plain integers and they may
be composed of base and offset. But, in that case as well, the base
address of the segment (stack segment ) should be the same as both
the local variables are on the same stack (although the stack frames
are different). So, why their comparison is illegal ?


Typical C implementations use a stack, but there's no requirement to.
An implementation is free to store each function call's variables in a
different segment for example. So the standard doesn't require
comparisons (except equality comparisons) to work.

For typical implementations on current hardware, comparing the
addresses will do what you expect. If I remember correctly, Doug
Gwyn's "portable" alloca() used this method.

-- Richard
Apr 19 '06 #9
ju**********@yahoo.co.in wrote:
Keith Thompson wrote:
Relational operators on pointer values are defined only when both
pointers point within the same object (or just past the end of it).
A comparison like "&local1 < &local2" invokes undefined behavior.

It's likely to work as you expect on most implementations, but it's
not guaranteed by the standard.


Thanx a lot for your help. I have one more doubt that why it is illegal
to
compare the addresses of two local variables ?


It's not /illegal/. It's /undefined/.

I believe it's to allow implementations the leeway to implement pointer
comparision as efficiently as possible. By only specifying the behaviour
of pointer comparison when the pointers point into the same array [1],
the implementation is at liberty to assume compared pointers point
into the same entity.

So a (some) segmented implementation(s) might only compare the offset
and not the segment, if that was more efficient than comparing both.

Also note that local variables can be stored in any order the compiler
wants, independantly of the order the stack grows in. EG it might put
the most-used locals in registers, the next-most-used in stack locations
that are cheap to access [2], and the less-used ones in more expensive
locations. The declaration `int a, b;` could put `a` before or after
`b`, depending on their use patterns. The compiler is not obliged
to discard efficiency just because of an accident of source ordering.

[1] Or array-like entity - mallocated store - or one-past-the-end.

[2] Like `(sp)` on the PDP-11.

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie automatically."
Apr 19 '06 #10
On Wed, 19 Apr 2006 04:28:45 -0700, ju**********@yahoo.co.in wrote:

Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
> Hi all,
>
> Is there any way by which we mat determine the direction of stack
> growth (from
> higher address to lower address Or from lower address to higher
> address) ?


There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably. But, can you suggest some way for
a
specific implementation (as for instance motorola's 68k processor) ?

You could look at the assembly code output from the compiler.
Duncan

Apr 19 '06 #11
Chris Dollin wrote:
I believe it's to allow implementations the leeway to implement pointer
comparision as efficiently as possible. By only specifying the behaviour
of pointer comparison when the pointers point into the same array [1],


<fx:mumble face="red">can't remember whether this is for equality too</>.

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie automatically."
Apr 19 '06 #12
"ju**********@yahoo.co.in" wrote:

Chris Dollin wrote:
ju**********@yahoo.co.in wrote:
Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?


There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably. But, can you suggest some way for
a specific implementation (as for instance motorola's 68k processor) ?


Well, if you know the processor, you know that stack order already. :-)

However, something like this _may_ work in a stack-based implementation:

#include <stdio.h>

void foo(int *);
int main(int argc,char *argv[])
{
int i;
foo(&i);
}
void foo(int *ii)
{
int j;
if ( &j < ii )
printf("I think the stack grows down.\n");
else if ( &j > ii )
printf("I think the stack grows up.\n");
else
printf("I'm really confused now.\n");
}

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 19 '06 #13
In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
Well, if you know the processor, you know that stack order already. :-)


Not at all! Many processors make it easier to run stacks in some
particular direction, but it would be quite possible to write a C
implementation that didn't follow the convention.

-- Richard
Apr 19 '06 #14
Richard Tobin wrote:

In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
Well, if you know the processor, you know that stack order already. :-)


Not at all! Many processors make it easier to run stacks in some
particular direction, but it would be quite possible to write a C
implementation that didn't follow the convention.


True, but probably the only reason to do so would be to break programs
that assumed otherwise. :-)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Apr 19 '06 #15
In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
Richard Tobin wrote:
In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
>Well, if you know the processor, you know that stack order already. :-)
Not at all! Many processors make it easier to run stacks in some
particular direction, but it would be quite possible to write a C
implementation that didn't follow the convention.

True, but probably the only reason to do so would be to break programs
that assumed otherwise. :-)


Or to increase compatability with programs that assumed a particular
direction.

Another point is that some processors are able to run in multiple modes,
not all of which will necessarily have the same optimal stack direction.

[I'm thinking of the MIPS ability to run either endian; I don't recall
whether that has any effect on best stack growth or upon "conventional"
stack growth.]
--
All is vanity. -- Ecclesiastes
Apr 19 '06 #16
Chris Dollin <ch**********@hp.com> writes:
Chris Dollin wrote:
I believe it's to allow implementations the leeway to implement pointer
comparision as efficiently as possible. By only specifying the behaviour
of pointer comparison when the pointers point into the same array [1],


<fx:mumble face="red">can't remember whether this is for equality too</>.


Equality comparison is defined for pointers to distinct objects (and
yields 0).

C99 6.5.9:

Two pointers compare equal if and only if both are null pointers,
both are pointers to the same object (including a pointer to an
object and a subobject at its beginning) or function, both are
pointers to one past the last element of the same array object, or
one is a pointer to one past the end of one array object and the
other is a pointer to the start of a different array object that
happens to immediately follow the first array object in the
address space.

And n1124 adds:

For the purposes of these operators, a pointer to an object that
is not an element of an array behaves the same as a pointer to the
first element of an array of length one with the type of the
object as its element type.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 19 '06 #17
Richard Tobin wrote:
In article <44***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
Well, if you know the processor, you know that stack order already. :-)


Not at all! Many processors make it easier to run stacks in some
particular direction, but it would be quite possible to write a C
implementation that didn't follow the convention.


On one processor I've used it is just as easy (and has no performance
penalty) to make the stack grow in either direction.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 19 '06 #18
On Wed, 19 Apr 2006 10:43:35 -0400, Kenneth Brody
<ke******@spamcop.net> wrote in comp.lang.c:
"ju**********@yahoo.co.in" wrote:

Chris Dollin wrote:
ju**********@yahoo.co.in wrote:

> Hi all,
>
> Is there any way by which we mat determine the direction of stack
> growth (from
> higher address to lower address Or from lower address to higher
> address) ?

There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?


I know, this cannot be done portably. But, can you suggest some way for
a specific implementation (as for instance motorola's 68k processor) ?


Well, if you know the processor, you know that stack order already. :-)


Not really. ARM, for example, can build stacks in either direction,
with no difference in code size or execution speed.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Apr 20 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Serrano | last post by:
I have a question about altering tables and growth of the database and transaction log. I have a database which is approximately 35GB. I had to make a change to a column in the largest table...
10
by: Generic Usenet Account | last post by:
I have worked out a very simple method for tracking the "memory growth" of a process at run time. It involves a header file and a shell script. Here's the header file: ////////// Header File...
6
by: masri999 | last post by:
Hello, I need to monitor every 15 minutes growth in data file and log file . Since mdf and intial file sizes are set to high value, measuring these values at 15 min interval will not provide the...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
6
by: Adam Warner | last post by:
Hi all, Is this a (C99) portable way to detect whether the C stack grows upwards (1) or downwards (-1)? #include <stdio.h> int stack_direction=0; void detect_stack_direction(void *...
13
by: Ben R. Bolton | last post by:
The documentation indicates that the threads "default stack size" is 1MB. The work "default" implies that it can be changed. Is it possible to change the StackSize in .NET? If so how? Is it...
4
by: Sagaert Johan | last post by:
Hi Is this something for C# V2 ? Queue msgqueue =new Queue (); //fill some data in the queue here with this function & keep only the last 5 events void addtolog(string msg) {
3
by: sindhu sweet | last post by:
How to find whether the stack is progressing in forward or backward direction using C?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.