473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know that stack is growing upward/downward?

Hi all,

I wanted to know whether the stack in a C program is growing upwards
or
downwards.So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ?

#include <stdio.h>
int main(void)
{
int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeo f(int));

printf( buf1 ? buf2 ? "Downward" : "Backward") ;

return 0;
}

Thanks...
Nov 14 '05 #1
41 6033
In article <17************ **************@ posting.google. com>,
Nitin Bhardwaj wrote:
I wanted to know whether the stack in a C program is growing upwards
or
downwards.So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ?

#include <stdio.h>
int main(void)
{
int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeo f(int));

printf( buf1 ? buf2 ? "Downward" : "Backward") ;

return 0;
}


malloc et all don't allocate on the stack.
The direction of the stack is undefined in C. Refer to OS or hardware
specific documentation.

Ariane
--
We remember the song,
But not the hand that wrote it.
Nightwish -- Dead Boys Poem

Nov 14 '05 #2
In article <17************ **************@ posting.google. com>,
Nitin Bhardwaj <ni************ *@hotmail.com> wrote:
I wanted to know whether the stack in a C program is growing upwards
or downwards. So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ? int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeo f(int));

printf( buf1 ? buf2 ? "Downward" : "Backward") ;


This shows you the order of two objects allocated on the heap (which is
likely not to be consistent). You need to compare the addresses of local
variables, e.g.

char *stack_dir(void )
{
int a;
return stack_dir_helpe r(&a);
}

char *stack_dir_help er(int *a)
{
int *b;
return b < a ? "down" : "up";
}

This is of course not theoretically portable, but is likely to work on all
machines where the answer would be useful.

The only program I recall seeing that needed to know the stack
direction was Doug Gwyn's "portable alloca".

-- Richard
Nov 14 '05 #3
Nitin Bhardwaj wrote on 02/08/04 :
I wanted to know whether the stack in a C program is growing upwards
or
downwards.


There is no standard way to determine that. The answer is on your
compiler's documentation.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #4
Richard Tobin wrote:
char *stack_dir_help er(int *a)
{
int *b;
return b < a ? "down" : "up";
}

This is of course not theoretically portable,
"Undefined behavior" is the term usually used
to describe code like that in this newsgroup.
but is likely to work on all
machines where the answer would be useful.


I doubt it.
Your code attempts to compare the value of a,
which is a passed in argument,
against the value of b,
which is an uninitialized variable with an indeterminate value.

--
pete
Nov 14 '05 #5
In article <41***********@ mindspring.com> ,
pete <pf*****@mindsp ring.com> wrote:
char *stack_dir_help er(int *a)
{
int *b;
return b < a ? "down" : "up";
}
Your code attempts to compare the value of a,
which is a passed in argument, against the value of b,
which is an uninitialized variable with an indeterminate value.


Oops, of course that should be

int b;
return &b < a ? "down" : "up";

(I even compiled and ran the original to check for mistakes, but some
mistakes still produce the expected output!)

-- Richard
Nov 14 '05 #6
Richard Tobin <ri*****@cogsci .ed.ac.uk> wrote:
In article <41***********@ mindspring.com> ,
pete <pf*****@mindsp ring.com> wrote:
char *stack_dir_help er(int *a)
{
int *b;
return b < a ? "down" : "up";
}
Your code attempts to compare the value of a,
which is a passed in argument, against the value of b,
which is an uninitialized variable with an indeterminate value.

Oops, of course that should be int b;
return &b < a ? "down" : "up"; (I even compiled and ran the original to check for mistakes, but some
mistakes still produce the expected output!)


The standard still tells you explicitely that you invoke undefined
behavior here, because 'a' and '&b' aren't pointers to members of
the same aggregate or union object (or one points to an array element
and the other points to the element directly after the same array).
Moreover, nothing forces the compiler to use a stack at all. So this
"solution" is about as safe as telling someone asking how to find
out the sequence in which function arguments are evaluated to use

int i = 0;
printf( "%d %d\n", ++i, ++i );

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #7
In article <2n************ @uni-berlin.de>,
<Je***********@ physik.fu-berlin.de> wrote:
The standard still tells you explicitely that you invoke undefined
behavior here, because 'a' and '&b' aren't pointers to members of
the same aggregate or union object (or one points to an array element
and the other points to the element directly after the same array).
Moreover, nothing forces the compiler to use a stack at all.


We all know that. If you've got nothing useful to say, stay silent.

I imagine someone coming up to you in the street, and asking the way
to the post office. Instead of telling them, you point out that
there's no guarantee that there is a port office in this town, and
tell them that asking a stranger might lead to undefined behaviour.

There seems to be something about this newsgroup which produces an
almost religious unhelpfulness. It never used to be like that.

*If* your C implementation uses a stack - and if you say yours
doesn't, you're probably lying - then the code I gave will tell you
which way it goes. There are several reasons for wanting to know,
one of which I gave as an example, and another of which is just an
interest in how your machine works.

-- Richard
Nov 14 '05 #8
Richard Tobin <ri*****@cogsci .ed.ac.uk> wrote:
In article <2n************ @uni-berlin.de>,
<Je***********@ physik.fu-berlin.de> wrote:
The standard still tells you explicitely that you invoke undefined
behavior here, because 'a' and '&b' aren't pointers to members of
the same aggregate or union object (or one points to an array element
and the other points to the element directly after the same array).
Moreover, nothing forces the compiler to use a stack at all.

We all know that. If you've got nothing useful to say, stay silent.
Are you sure? Ask the OP if he knows about that restriction. I for
one didn't know about such things at all when I learned C by myself
and only became aware of such problems by reading this newsgroup
_because_ people pointed them out again and again.
I imagine someone coming up to you in the street, and asking the way
to the post office. Instead of telling them, you point out that
there's no guarantee that there is a port office in this town, and
tell them that asking a stranger might lead to undefined behaviour.
Well, what you were telling the OP was: "Drive through that one-way
road to the post office." and not even mention that there's a the
big, fat sign that forbids to drive in that direction.
There seems to be something about this newsgroup which produces an
almost religious unhelpfulness. It never used to be like that.
Why is pointing out that the results of that operation can give
you completely wrong results unhelpful? The compiler is free to
produce code that exactly does what you expect it to do but it's
equally free to produce code that results in output that says the
stack is growing upwards while in reality its growing downward. And
it also is allowed to produce code that reformats the hard disk...

As far as I know this group it was standard to point out such
potential problems carefully. And I for one would like it to
stay it that way because otherwise whatever I read here would
have be taken with too much a grain of salt to be useful anymore.
*If* your C implementation uses a stack - and if you say yours
doesn't, you're probably lying - then the code I gave will tell you
which way it goes. There are several reasons for wanting to know,
one of which I gave as an example, and another of which is just an
interest in how your machine works.


Wanting to know is completely legitimate. But proposing methods to
find out that are not guaranteed to work without pointing out the
possible pitfalls isn't that helpful in my book.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #9

On Mon, 2 Aug 2004, Richard Tobin wrote:

<Je***********@ physik.fu-berlin.de> wrote:
The standard still tells you explicitely that you invoke undefined
behavior here, because 'a' and '&b' aren't pointers to members of
the same aggregate or union object (or one points to an array element
and the other points to the element directly after the same array).
Moreover, nothing forces the compiler to use a stack at all.


We all know that. If you've got nothing useful to say, stay silent.

I imagine someone coming up to you in the street, and asking the way
to the post office. Instead of telling them, you point out that
there's no guarantee that there is a [post] office in this town, and
tell them that asking a stranger might lead to undefined behaviour.


Well, that would be false --- in general, asking a stranger does
/not/ lead to undefined behavior. :) Jens' response, while less
helpful than it could have been,[1] was right on the money.

-Arthur

[1] - For example, Jens could have explicitly pointed out that
where your code had
char *stack_dir_help er(int *a)
{
int b;
return &b < a ? "down" : "up";
}
you almost certainly meant to write
char *stack_dir_help er(int a)
{
int b;
return &b < &a ? "down" : "up";
}


which will "correctly" return "down" on common unoptimized Wintel
implementations . I have no idea what it will produce for the average
'gcc -O2', in which it is common for both 'a' and 'b' to be stored
in 80x86 machine registers, let alone on a different architecture
entirely!
Nov 14 '05 #10

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

Similar topics

21
3011
by: puzzlecracker | last post by:
Guys - Is it possible to find which way system stack grows? Perhaps, I am not precise enough: When a function is called, the return address of (callee) function is put on the stack. Thus, the question is the direction where that stack heading (address increasing or decreasing). How would you implement this in cpp? any suggestion.
24
6553
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows 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...
20
9177
by: deepak | last post by:
Hi All, In C I heard the stack grows from top to bottom and heap from bottom to top. Is it compiler depend?
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.