sunny said:
Hi All
Is there any way to determine stack and heap size, during runtime.
i.e can we predict stack overflow. etc
IMHO this is nothing the C standard can address.
You CAN on many (but not all) systems do a little math on pointer
variables to figure out how much the stack has grown, something like:
unsigned long int hi;
void deeplynested(){ unsigned long int lo, len;
lo = (unsigned int) &lo;
len = hi - lo;
printf("stack is about %d bytes\n", len );
}
int main ( .... ){ int mainvar; hi = (unsigned long int) &mainvar;
.... call stuff... return 0;}
.... as for figuring out heap usage, you could write a wrapper for
malloc() that keeps track of the amount allocated so far. But this
will not tally the overhead per block for the usual block headers, so
it will be an undercount to some extent. And keeping track of free()ed
memory gets a bit tricky. Plus its hard to link in your wrapper so all
library routines go through the wrapper. Ugh.
But it *can* be done and is very handy to find places where there's
excessive stack or heap usage.