wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> stack direction
(Message started by: first on Aug 3rd, 2007, 8:15am)

Title: stack direction
Post by first on Aug 3rd, 2007, 8:15am
write a c program to find whether a stack is
progressing in forward or reverse direction.

Title: Re: stack direction
Post by SMQ on Aug 3rd, 2007, 8:32am
#include <stdio.h>

void show_stack_dir(int * p) {
 printf("%s\n", p < (int*)(&p) ? "up" : "down");
}

int main(int argc, char ** argv) {
 show_stack_dir(&argc);
 return 0;
}


--SMQ

Title: Re: stack direction
Post by Skandh on Aug 19th, 2007, 11:19pm

on 08/03/07 at 08:32:12, SMQ wrote:
#include <stdio.h>

void show_stack_dir(int * p) {
 printf("%s\n", p < (int*)(&p) ? "up" : "down");
}

int main(int argc, char ** argv) {
 show_stack_dir(&argc);
 return 0;
}


--SMQ



Can you please elaborate how this code is working?
Thank you.

Title: Re: stack direction
Post by gotit on Aug 20th, 2007, 1:26am
I think this is what the code means:

When main() is called, argc is pushed in the stack and thus will have a memory address(denoted by p
in the code).When show_stack_dir() is called this address itself is put in the stack and its address is denoted by &p in the code.

Now if p<(int*)&p,it implies that the argument to  show_stack_dir() is at a higher memory location than the argument to main() which in turn implies that the stack frame allocated to show_stack_dir() is at a higher memory location than the one allocated for main().Thus the stack grows  upward.Othereise,it grows downwards.

plz correct me if i am wrong....

Title: Re: stack direction
Post by SMQ on Aug 20th, 2007, 6:22am

on 08/20/07 at 01:26:21, gotit wrote:
plz correct me if i am wrong....

You got it exactly.

There is, however, one big caveat, which is that the C specification doesn't define how pointer comparisons have to work, just that they have to be consistent -- i.e. &myArray[1] should always be greater than &myArray[0].  In some older computer architectures it made most sense for arrays to be allocated "backwards", with the lower-numbered elements at higher memory addresses, and so on those architectures the meaning of pointer comparisons is reversed (and, of course, pointer arithmetic is reversed as well).  In those cases the above program would give the opposite of the expected result.

One could attempt to work around that by casting the pointers to, e.g., an unsigned long, but there's again no guarantee that will produce the expected results on all architectures, and it's exactly those computers with inverted pointer arithmetic that it's least likely to work on, so...

The truth is that there's no 100% guaranteed way to determine the direction of growth of the program stack from within a C program, but the above is likely to work on the vast majority of computers.

--SMQ

Title: Re: stack direction
Post by towr on Aug 20th, 2007, 6:31am

on 08/20/07 at 06:22:57, SMQ wrote:
but the above is likely to work on the vast majority of computers.
Since the majority of computers have the same architecture, you could just always say 'down' :P

Title: Re: stack direction
Post by SMQ on Aug 20th, 2007, 7:18am
True enough, and since the stacks on Intel's x86 and Itanium families, IBM's PPC family, Motorola's 68xx family, Sun's Sparc family, and DEC's Alpha family of processors all grow down, I think that would cover all the hardware most of us are likely to encounter.

Oh well, I concede: int main(void) { return printf("down\n"); } is much shorter than my code above ... and likely just as useful.

--SMQ



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board