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.