473,467 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can we determine stack size & Heap size at runtime ?

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
5 37070

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oleg Konovalov | last post by:
Hi, I am running applets in Java Plug-in 1.4.2 under IE6 and often receive OutOfMemory exceptions (dealing with large file - video clips, etc.) although according to option M, almost 50% (out...
4
by: Skyhorse | last post by:
In fact, this is not a programming problem, but I think I can seek help here Sometimes I saw terms about multimedia such as 'prebuffering', 'buffer size' , but I don't know their actual meaning....
4
by: ivan | last post by:
Hi, How can we calculate the stack and heap size requried by a C program. Is there any specific formula used? Please suggest. regards, Ivan
9
by: shine | last post by:
what is the difference between a heap and a stack?
3
by: | last post by:
Hi,all I want to change Control's size&location in runtime ,(C#) but i dont know how to do! :(so, can you provide me with document about this. my email: zhonghua@rinpak.com.cn ...
9
by: Zach | last post by:
Could someone please illustrate this with some ANSI C code? :-) Zach
11
by: Andy Watson | last post by:
I have an application that scans and processes a bunch of text files. The content I'm pulling out and holding in memory is at least 200MB. I'd love to be able to tell the CPython virtual machine...
7
by: Raman | last post by:
Hi All, Could any one tell me, how can I determine/Change size of heap on per- process basis on Unix based systems. Thanks. Regards
6
by: ayatsynych | last post by:
Hi, Is there any way to determine at run-time if block of memory allocated on stack or on heap? For example: BYTE* pData = new BYTE; BYTE data; BYTE* pData1 = &data; //Hot to determine...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.