473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to calculate the stack and heap size

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

Nov 15 '05 #1
4 16644
ivan wrote:
Hi,

How can we calculate the stack and heap size requried by a C program.
Is there any specific formula used?
There is no standard way to calculate the stack size required, since
1) The C standard does not require a stack
2) You can't tell whether local variables will have memory associated at
all, they might just be kept in registers or the compiler may find a
way to eliminate some all together
3) There could be any or no padding between parameters

You can estimate it if you know how your implementation does things,
although if you use recursion this can be very difficult.

There is no standard way to determine how much heap us used because
1) The C standard does not require a heap
2) Different heap management schemes have different overheads
3) It depends on how good your implementation is at reusing freed memory

Again, if you know your implementation well enough it may be possible to
estimate this.
Please suggest.


I suggest that you ask somewhere dedicated to your system since it is
highly system specific. This suggestion is for your benefit as much as ours.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #2
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
ivan <iv******@gmail .com> wrote:

How can we calculate the stack and heap size requried by a C program.
Is there any specific formula used?


There is no general solution, since a C program can written which
uses a random amount of any resource.

Once you accept that sad fact, it's often possible to estimate
the usage by running the program multiple times, feeding it
different kinds and amounts of input, and observing its behavior
using operating-system specific tools.

Another method is to examine the program and add up the various
sizes of local variables and heap allocations. Some programs
are simple enough that you can simply see that they will use
a certain amount of a certain resource, constant or varying by
the amount or type of input. If there are is no recursion,
VLA's or alloca() calls, there should be a calculable upper
limit on stack space. If there are no malloc/calloc/realloc
calls, there should be a calculable upper limit on heap space.
Nov 15 '05 #3
"ivan" <iv******@gmail .com> wrote

How can we calculate the stack and heap size requried by a C program.
Is there any specific formula used?

Look for any recursive functions. If you have none, then stack space is
almost certainly trivial, unless someone has been putting big arrays on the
stack. In that case the size is dominated by the size of the big arrays.
If you have recrusive fucntions you need to calculate the depth, and then
try to work out how much memory is taken.

In the case of the heap, you control the parameter passed to malloc. This
can be misleading if you allocate a lot of small items, because the size
will often be rounded up. You also need to include a few bytes for control
information.

These are rough rules. A perverse compiler writer could make a char take up
a whole kilobyte and remain within the standard. A non-perverse one may give
a list of twenty chars 8 bytes each, becuase that happens to be more
efficient for accesses. However normally you don't want an exact figure, but
just some idea of how much memory your program is using.
Nov 15 '05 #4

In article <dj**********@n wrdmz02.dmz.ncs .ea.ibs-infra.bt.com>, "Malcolm" <re*******@btin ternet.com> writes:
"ivan" <iv******@gmail .com> wrote

How can we calculate the stack and heap size requried by a C program.
Is there any specific formula used?

Look for any recursive functions. If you have none, then stack space is
almost certainly trivial, unless someone has been putting big arrays on the
stack. In that case the size is dominated by the size of the big arrays.
If you have recrusive fucntions you need to calculate the depth, and then
try to work out how much memory is taken.


I know what you mean here, but some readers might take "recursive
functions" to mean only functions that call themselves, and what this
analysis really needs is to discover whether the call graph for the
program contains any cycles: function A calls function B which calls
C etc until somewhere down the line A is called again.

Obviously there are potential complications such as calls through
function pointers.

And, as you also said (more or less), this is only a heuristic; the C
language gives us no rules for actually determining how much storage
is required for a single function call (or where it will come from),
much less the high-water mark for such storage over any potential (or
plausible) exeuction of a program.

--
Michael Wojcik mi************@ microfocus.com

Don't forget your fighting spirit at each balls you pitch!
-- Tornado Boy Volunteer Staff International
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30102
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
17
5054
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
2
2490
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
11
2317
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter? Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices...
10
8349
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But what about the global space? Is that the same as the heap space or is that still a form of special stack? Because once I've seen someone declare a 1 megabyte char array in global space like this: char s;
20
3504
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
9
3388
by: shine | last post by:
what is the difference between a heap and a stack?
9
7331
by: Ajay | last post by:
Hi all, Can I know what is the stack space and heap space allocated by the compiler.Can i increase it or decrease it.if yes,pleae tell me theway to do it.Thanks in advance. Cheers, Ajay
11
2926
by: Nehil | last post by:
I would like to know which is dynamic in nature. if i refer the C memory model (Richard Steven), it is shown that both stack and heap grow towards each other. Now, can one go into other's area and hence effecting the size of other memory area. Does any limit exist upto which a stack or a heap can grow. and if it is there then who decides the limit? plz clarify.
0
9565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.