Author |
Topic: stack direction (Read 2487 times) |
|
first
Newbie
Posts: 22
|
|
stack direction
« on: Aug 3rd, 2007, 8:15am » |
Quote Modify
|
write a c program to find whether a stack is progressing in forward or reverse direction.
|
|
IP Logged |
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: stack direction
« Reply #1 on: Aug 3rd, 2007, 8:32am » |
Quote Modify
|
#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
|
« Last Edit: Aug 3rd, 2007, 1:20pm by SMQ » |
IP Logged |
--SMQ
|
|
|
Skandh
Junior Member
Gender:
Posts: 77
|
|
Re: stack direction
« Reply #2 on: Aug 19th, 2007, 11:19pm » |
Quote Modify
|
on Aug 3rd, 2007, 8:32am, 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.
|
|
IP Logged |
I wanna pull by legs!!!
|
|
|
gotit
Uberpuzzler
Gender:
Posts: 804
|
|
Re: stack direction
« Reply #3 on: Aug 20th, 2007, 1:26am » |
Quote Modify
|
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....
|
« Last Edit: Aug 20th, 2007, 1:29am by gotit » |
IP Logged |
All signatures are false.
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: stack direction
« Reply #4 on: Aug 20th, 2007, 6:22am » |
Quote Modify
|
on Aug 20th, 2007, 1:26am, 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
|
|
IP Logged |
--SMQ
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: stack direction
« Reply #5 on: Aug 20th, 2007, 6:31am » |
Quote Modify
|
on Aug 20th, 2007, 6:22am, 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'
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: stack direction
« Reply #6 on: Aug 20th, 2007, 7:18am » |
Quote Modify
|
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
|
|
IP Logged |
--SMQ
|
|
|
|