Memory Management in C
Memory blocks are allocated for program entities during execution phase.
Memory allocation is of three types in C program:
Static Memory Allocation
Static allocation happens when you declare a static or global variable. The space is allocated once, when your program is started and remain reserved till the end of the program. These variables are stored in a data segment. A data segment is a fixed size area in the memory set aside for these variables.
Automatic Memory Allocation
Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered and is freed when that compound statement is exited.These variables are stored in a stack. A stack is an area of memory that starts out small and grows automatically up to some predefined limit. The stack is kept in the region of memory known as the stack segment.
Dynamic Memory Allocation
Memory is allocated dynamically to the entities of the program. Programmer has freedom to allocate and free the memory for the program entities. Such a memory allocation is called dynamic memory allocation.This memory area is known as a heap. A heap is another area that starts out small and grows, but it grows only when there is an explicit call to some memory allocation functions like malloc, calloc or realloc. The heap, like the stack, has a limit on how much it can grow.
Below are some library functions provided in <stdlib.h>
to deal with dynamic memory allocation needs.
void *malloc( size_t size );
- The
malloc
function allocates the memory space for an object whose size is specified by the parameter size. - The
malloc
function returns a void pointer to the first byte of the allocated space, if successful. malloc
function does not initialize allocated space to any value.- If the memory space cannot be allocated, a null pointer is returned.
malloc
is faster thancalloc
as it does not initialize allocated space.
void *calloc(size_t n, size_t size);
- The
calloc
function allocates the memory space for an array of n objects, each of whose size is specified by the parameter size. - Unlike
malloc
,calloc
initializes every byte of the block allocated with the value 0. - The
calloc
function returns a void pointer to the first element of the allocated elements, If successful. - If the memory space cannot be allocated, a null pointer is returned.
calloc
takes little longer thanmalloc
because of the extra step of initializing the allocated memory by zero.
void *realloc(void *ptr, size_t size);
- When dynamically allocated memory appears too large or too small then
realloc
function can be used to resize allocated memory without losing old data realloc
function reallocates the given area of memory. It must be previously allocated bymalloc
,calloc
orrealloc
.- The
realloc
function accepts two arguments, the first argument ptr is a pointer to the first byte of memory that was previously allocated and second argument is the new size of the block in bytes, which may be smaller or larger than the original size. - If the memory for the new object cannot be allocated, the old object is not deallocated and its value remains unchanged.
- The
realloc
function returns a void pointer to the new object, or a null pointer if the new object cannot be allocated. - If ptr given to the realloc function is a null pointer, the
realloc
function behaves like themalloc
function. - If the second argument passed to the realloc function is zero, it behaves like a
free
function.
void free(void *ptr);
- Dynamically allocated memory does not get freed on their own. You must explicitly use
free
function to deallocate the memory. free
function deallocates the memory previously allocated by a call tomalloc
,calloc
orrealloc
.- The
free
function returns no value. - To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.