Allocating and releasing dynamic memory
Dynamic memory is allocated and released (deallocated) only at very explicit points by a program. It doesn't happen automatically; it doesn't happen by accident or by chance. You make this happen when you call specific C Standard Library calls to allocate and release dynamic memory.
Allocating dynamic memory
Memory allocation routines are declared in stdlib.h
and are a part of the C runtime library. There are two very similar allocation routines, malloc()
and calloc()
, which are used to allocate a new block of memory from the heap. The main difference between malloc()
and calloc()
is that calloc()
clears the memory block it allocates, whereas malloc()
only does allocation. There is a third routine, realloc()
, which is used to resize an existing block of heap memory. These functions have the following prototypes:
void* malloc( size_t size );
void* calloc( size_t...