How to calculate the stack and heap size

I

ivan

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
 
F

Flash Gordon

ivan said:
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.
 
A

Anonymous 7843

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

Malcolm

ivan said:
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.
 
M

Michael Wojcik

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.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top