On 7 Jan, 21:53, (e-mail address removed) (Gordon Burditt) wrote:
Gordon seems to have lost the attribution lines. >> is Stephen Sprunk
and >>> is sacindia2000.
Gordon doesn't "lose" attribution lines. He deliberately deletes them.
[...]
Stephen said:
No, I think Stephen is right. The stack grows when check_stack is
called. p is &foo, a local variable in main, whereas q is &bar, a
local variable in check_stack.
You say Stephen is right, and then you contradict what he said. The
standard doesn't guarantee that there's such a thing as "the stack".
Function calls imply a stack-like data structure of some kind, in the
sense that things are allocated and deallocated in a last-in first-out
manner. But there's no guarantee, or even implication, that these
allocations occur contiguously or in a consistent direction. And no
portable program can even test this, since any relational comparison
between two pointers to distinct objects (that are not part of some
containing object) invokes undefined behavior.
Having said that, *most* C implementations do use a contiguous stack
that grows in a particular direction within a linear address space,
and *most* C implementations implement pointer comparison in a way
that reflects this linear address space. So at least some of the
programs that have been posted in this thread (though not the original
one) can be used to determine the direction in which "the stack"
grows. If you have a need for this information, and you don't mind
your program not being portable to the minority of implementations
that don't behave this way, then you can probably use the techniques
presented here.
But if you think you need this information (other than out of simple
curiosity), you're probably mistaken. There may be valid reasons for
a program to need to know which way the stack grows, but off the top
of my head I can't think of any.
[Permission to quote this article without proper attribution is
expressly denied.]