heap - WordPress.com

brickcompetitiveSoftware and s/w Development

Dec 14, 2013 (3 years and 10 months ago)

134 views

4.11 THE C HEAP USAGE

The C runtime heap is an uninitialized area of data memory that is used for dynamic

memory allocation using the standard C library dynamic memory management

functions,
calloc
,
malloc
and
realloc
. If you do not use any of these
functions,

then you do not need to allocate a heap. By default, a heap is not created.

If you do want to use dynamic memory allocation, either directly, by calling one of the

memory allocation functions, or indirectly, by using a standard C library input/o
utput

function, then a heap must be created. A heap is created by specifying its size on the

linker command line, using the
--
heap
linker command
-
line option. An example of

allocating a heap of 512 bytes using the command line is:

pic30
-
gcc foo.c
-
Wl,
--
hea
p=512

The linker allocates the heap immediately below the stack (Figure 4
-
2).

If you use a standard C library input/output function, then a heap must be allocated. If

stdout
is the only file that you use, then the heap size can be zero, that is, use the

co
mmand
-
line option:

-
Wl,
--
heap=0

If you open files, then the heap size must include 40 bytes for each file that is simultaneously

open. If there is insufficient heap memory, then the
open
function will return an

error indicator. For each file that should be

buffered, 514 bytes of heap space is

required. If there is insufficient heap memory for the buffer, then the file will be opened

in unbuffered mode.