Memory Management

Memory Management

Computer memory is a limited resource and we have to manage it efficiently. The process by which computer programs are assigned with physical or virtual memory space is called memory allocation.

The memory allocation can be done either before or at the time of program implementation. There are two techniques for memory allocation: static memory allocation and dynamic memory allocation.

Static Memory Allocation

In this type of allocation, the compiler allocates a fixed amount of memory during compile time and the operating system internally uses a data structure known as stack to manage the memory.

Exact memory requirements must be known in advance as once memory is allocated it can not be changed.

Loading...

Dynamic Memory Allocation

In this type of allocation system memory is managed at runtime. Dynamic memory management in C programming language is performed using four functions named malloc(), calloc(), realloc(), and free(). These four functions are defined in the C standard library header file <stdlib.h>. It uses the heap space of the system memory.

Let’s know about each function in detail:

malloc()

malloc() function is used to allocate a block of memory in the heap. It allocates the user a specified number of bytes but does not initialize. Once allocated, the program accesses this block of memory via a pointer that malloc() returns.

The default pointer returned by malloc() is of the type void but can be cast into a pointer of any data type. However, if the space is insufficient for the amount of memory requested by malloc(), then the allocation fails and a NULL pointer is returned.

Loading...

Since the size of int is 4 bytes, the above statement will allocate 20 bytes (4 * 5) of memory.

calloc()

calloc() function is used to dynamically allocate the specified number of blocks of memory of the specified type. It is very much similar to malloc() but the difference is that it initializes each block with a default value 0 and it has two parameters as compared to malloc() which only has one parameter.

Loading...

The above statement allocates contiguous space in memory for 5 elements each with the size of the int.

realloc()

realloc() function is used to dynamically change the memory which was previously allocated. It means that if the memory that was previously allocated using the malloc() or calloc() function is not sufficient, so it can be reallocated using the realloc() function.

Loading...

free()

free() function is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() must be de-allocated manually by the user using the free() function when not in use. It helps to reduce the wastage of memory by freeing it.

Loading...
Interested in contributing to Codecademy Docs? Check out our Contribution Guide or leave us some feedback.

Learn C on Codecademy