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

How to calculate the stack and heap size

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
In article <11**********************@g49g2000cwa.googlegroups .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

P: n/a
"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

P: n/a

In article <dj**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>, "Malcolm" <re*******@btinternet.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 discussion thread is closed

Replies have been disabled for this discussion.