tags:

views:

164

answers:

5

So from my previous memmove question I would like to know how to find direction of stack growth.

  void stackDirection(int* i)
  {

     int j;

     if(&j>i)
         cout<<"Stack is growing up \n"<<endl;
     else 
         cout<<"Stack is growing down \n"<<endl;


   }
  int main()
  {    
      int i=1;

      stackDirtection(&i);

 }
+10  A: 

The stack may not grow up or down.

Each stack frame can potentially be allocated at random points inside the heap.
This is actually done in several OS to try and prevent stack smashing by malicious code.

The concept of a stack growing towards the heap is just an easy way to teach the concept of a stack (and of course early implementations did work this way as it was simple (no need to make something harder than you need when nobody is trying to break you)).

Martin York
@Martin Stack is created on the heap ? I dint get it ? OS seperates stack from heap right and divides them into pages right ? How can this be architecture dependent ? Because what I am referring here is logical addresses right and these logical addresses are just compiler dependent right?
brett
@Brett: from the perspective of the hardware, all memory is just memory. The OS can then choose to do whatever it wants.
Potatoswatter
@brett: In the old days stack and heap were separate entities and conceptually grew towards each other. But this was rather easy to corrupt and cause malicious code execution. So some clever people had a little think. Now a stack is just a series of frames (one for each function call). There is no technical reason they need to go one after the other (as the current one has a link back to the last). So why not just allocate stack frames in the heap using normal memory management routines. Conceptually: Unwinding the stack is freeing the current frame and following the chain back.
Martin York
@brett: It's probably slightly more complex than that but conceptually there is no reason each stack frame can not be allocated within the heap. The implementation details of the stack should make no difference to the executing code as long as function local variables are correctly destroyed on exit from the local scope. Its a classic OO design (The run time changed the implementation of a stack frame but the code did not need to know as the interface to the stack frame was identical. The only effected code was relying on the implementation details of the stack (like your code)).
Martin York
@Martin York Can you point me to some article or a book explaining me this please
brett
@brett: There is no standard way. What I am saying is you can not assume up or down or even distribution. How the stack is actually implemented is an implementation details that is left up to the compiler. Your best bet is to read your compilers specific documentation on how the stack is implemented.
Martin York
A: 

Your function depends on int* parameter which can point to anywhere, and also be NULL. It is better to compare addresses of two local variables.

Alex Farber
The compiler is free to allocate local variables in any order. Yes, it will construct and destroy them in the right order, but it can allocate space for them wherever it wants.
sharptooth
+1  A: 

Experiments such as this are unreliable, because you can run into exceptions. The optimiser can mess you up, or the system might use registers for parameters. If you really must know the stack direction, read the manual for your processor.

Notwithstanding that, unless you are writing an operating system or something really low-level, if you need to know the stack direction you are probably doing something ugly and horrible and should really reconsider your methods.

Michael J
A: 

This is outside the scope of the C++ standard. It an implementation defined behavior and probably depends on the specific OS/processor architecture etc.

It is best not to rely or depend on such details, unless you are into ETHICAL hacking :) and / or not so much concerned with portability

Chubsdad
A: 

Consider the following rewrite, which exposes one way in which a compiler may implement your code:

void stackDirection(int* i) { struct __vars_stackDirection { int j; } *__stackframe_stackDirection = malloc(sizeof(int));

 if(&(__stackframe.j) > i)
     cout<<"Stack is growing up \n"<<endl;
 else 
     cout<<"Stack is growing down \n"<<endl;

} int main() {
struct __vars_main { int i; } *__stackframe_main = malloc(sizeof(int));

  stackDirection(&(__stackframe.i));

}

You'll have to agree that __stackframe_stackDirection and __stackframe_main will be essentially random. The stack can grow up or down.

Worse, you are assuming a lineair model. Either a<b, b<a, or a==b. But for pointers this does not hold. All three comparisons a<b, b<a and a==b may be false at the same time.

MSalters