Mail Archives: djgpp/1996/05/29/03:51:52
A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk wrote:-
> int depth(){long j,i;for(i=long(&i+2),j=0;i;i=*((long*)i),j++);return j;}
> I wrote this short function. In djgpp (at least in version 1) it gives how
> many stack call levels deep you are ...
ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire) replied:-
> I'm curious... why the devil does this work? :-)
> (I'd expect it to die with SIGSEGV on the second pass through the loop!)
In djgpp (at least in version 1) the runtime stack seeems to grow downwards
from (0xffffffff or a bit less) in virtual store. j is the 1st variable in the
stack, and is the 2nd (as depth() has no args). The 4 bytes next above j (i.e.
the 0th 4 words in depth()'s local stack) is a pointer to the 0th word of the
next local stack below. This chain goes on until it reaches the bottom of the
stack, where there is a 4-byte zero right at the very bottom.
Note that (if there is a type zxcvbnm) adding n to a zxcvbnm* value actually
steps the pointer on n*sizeof(zxcvbnm) bytes.
- Raw text -