By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

Can we determine stack size & Heap size at runtime ?

P: n/a
Hi All

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

Aug 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

sunny wrote:
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.

Aug 16 '06 #2

P: n/a
MQ

sunny wrote:
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

Aug 16 '06 #3

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .com>,
MQ <mi**************@gmail.comwrote:
>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.
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.

--
Programming is what happens while you're busy making other plans.
Aug 16 '06 #4

P: n/a
"sunny" <su*******@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Aug 16 '06 #5

P: n/a
Stephen Sprunk wrote:
"sunny" <su*******@gmail.comwrote in message
>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.
Well, at least one out of four responses was accurate and topical.

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan" - "I listened to Toronto come back
from 3:0 in '42, I watched Boston come back from 3:0 in '04"
Aug 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.