Consider the following correspondence, which provides one way the compiler can 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 must agree that __stackframe_stackDirection and __stackframe_main will be essentially random. The stack can grow up or down.
Worse, you are assuming a lineup model. Either a<b, b<a, or a==b . But for pointers, this fails. All three comparisons a<b, b<a and a==b can be false at the same time.
Msalters
source share