Can we determine stack size & Heap size at runtime ?

S

sunny

Hi All

Is there any way to determine stack and heap size, during runtime.
i.e can we predict stack overflow. etc
 
A

Ancient_Hacker

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.
 
M

MQ

sunny said:
Hi All

Is there any way to determine stack and heap size, during runtime.
i.e can we predict stack overflow. etc

On Linux, you can get/set heap size with the sbrk() function. Not too
sure about stack size though. Linux probably has an initial stack size
that is probably kernel-version dependant. If you overflow the stack,
I think Linux will just make it bigger.

I don't known anything about Windows. You'd probably get a better
response on a forum for the platform you are developing

MQ
 
W

Walter Roberson

Is there any way to determine stack and heap size, during runtime.
i.e can we predict stack overflow. etc
[/QUOTE]
On Linux, you can get/set heap size with the sbrk() function.

To be overly picky, sbrk() does not allow you to get the heap
size, sbrk() returns an address. You can only calculate the heap
size if somehow you manage to sbrk() before any allocation is done;
otherwise the best you can do is to find the size change.

But [getting further offtopic] some of the Linux memory allocators
work by using mmap() to allocate memory segments that are -not-
in the heap; sbrk() cannot determine the size of those.
Not too
sure about stack size though. Linux probably has an initial stack size
that is probably kernel-version dependant.

I don't know about Linux; in SGI IRIX, the stack position is determined
by the linker (taking into account which ABI you are using).
You'd probably get a better
response on a forum for the platform you are developing

Indeed, we've already gone too platform-dependant in this thread.
 
S

Stephen Sprunk

sunny said:
Is there any way to determine stack and heap size, during runtime.
i.e can we predict stack overflow. etc

The C Standard does not require an implementation to have a stack or heap at
all, so there is no portable answer to your question.

Individual implementations may provide non-portable solutions; check in a
newsgroup for the system(s) you care about.

S
 
C

CBFalconer

Stephen said:
The C Standard does not require an implementation to have a stack
or heap at all, so there is no portable answer to your question.

Individual implementations may provide non-portable solutions;
check in a newsgroup for the system(s) you care about.

Well, at least one out of four responses was accurate and topical.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top