You can do it as follows:
1) In the main function, get the address of the first local variable:
void *stacktop;
int main(void)
{
int dummy;
stacktop = (void *)&dummy;
.....
}
2)
Now, you can write
long long stackdepth(void )
{
int dummy;
long long top = (long long)stacktop;
long long bottom = (long long)&dummy;
long long result = top - bottom;
if (result < 0)
result = -result;
return result;
}
Why would an implementation that does use a stack need to store the
first local variable of the current function on the top of that stack?
That seems to me to be a pretty big assumption in your code that you
failed to mention to the OP. While that may be the case in your
environment there is certainly no reason that it has to be implemented
in such a way as long as I can access the variable while it is in
scope I could just as easily put it last, or anywhere else I want for
that matter.
>
3)
Please ignore all stupid remarks like "this is not portable".
This is obviously not portable to any machine. It requires a
stack, and it supposes that an address fits in a long long.
....and that long long exists
4) Please ignore all stupid remarks like " this is undefined behavior
since you can't take the difference of two pointers that do not point to
the same object"
While he is at it why doesn't he ignore stupid responses to his
question that don't even answer what he was asking. While, in some
cases, this may give you the depth in bytes (or even possibly some
other unknown unit) it does not tell him in the units he asked about
pretty unabiguously when he stated "here i == 1."