C Dynamic Memory Allocation

Summary: in this tutorial, you will learn about C dynamic memory allocation mechanism and how to use C built-in functions to allocate memory.

Introduction to C Dynamic Memory Allocation

C Dynamic Memory Allocation

In C, the memory is managed statically, automatically or dynamically.

The static-duration variables are stored in the main memory and they exist during the program execution.

The automatic-duration variables, which you define in the function, are allocated on the stack. The automatic-duration variables are only available during the function execution.

Both static-duration and automatic-duration variables are required to allocate memory at compile time, which is seldom practical especially for large data objects .

Fortunately, C provides dynamic memory allocation mechanism that allows you to allocate memory at run-time. C gives you some useful functions e.g., malloc() , realloc() , calloc() and free() that help allocate memory based on the program’s needs.

Getting size of types using sizeof operator

Before allocating memory to a data object, you need to identify its size so that you can allocate the corresponding amount of memory.

To get the size of any data type e.g., int, float, array, structure, etc., you use the sizeof operator. The sizeof operator returns an amount of storage in bytes for any type.

The return type of the sizeof operator is size_t that is a separate type in C to represent amounts of memory. C uses a separate type size_t because the existing types are defined based on the target processor’s arithmetic capabilities, not the memory capabilities. The size_t is unsigned defined in the stddef.h header file. The maximum value of the size_t is defined as a macro constant ( SIZE_MAX) in the stdint.h file. It is at least 65535.

For example to get the size of integer type, you use the following expression:

It returns 4 bytes in a typical 32 bit machines.

The following program displays the sizes of common C data types.

Allocating memory

C provides you with three functions for allocating memory: malloc(), realloc() and calloc(). In addition, it also gives you the free() function for releasing memory which is no longer used.

Using the malloc() function

The prototype of the malloc() function is a follows:

The malloc() function allocate a block of memory with the size specify by the size_t. It returns It return a pointer to the allocated memory. If the memory could not be allocated or the size argument is 0, the malloc() function returns NULL.

The following example demonstrates how to use the malloc() function to allocate memory:

The sizeof(int) returns the size of an integer , which is 4 bytes, and multiply with 5 so the pi pointer is pointing to the first byte of 5 * 4 = 20 bytes memory block.

You should always check the return pointer from the malloc() function for NULL. See the following example:

Notice that the block of memory allocated by the malloc() function is uninitialized. If you want to initialize memory, you use memset() function after allocating memory or use calloc() function.

You should always call free() function to release the allocated memory. We will discuss about the free() later in the below section.

Using the calloc() function

The function header of the calloc() function is as follows:

The calloc() function allocates memory much like malloc() function except the following differences:

  • When you call the calloc() function, you need to pass 2 parameters: the first one specify the number of elements and the second one specify the size of each element. The product of these parameters determines the size of memory block being allocated.
  • The memory block allocated by the calloc() function is initialized to zero.

The following example demonstrates how to use the calloc() function to allocate memory for a dynamic array:

The following is the output:

Using realloc() function

The realloc() function allows you to change the size of the memory block allocated by the malloc() or calloc() function.

The realloc() function the size of the memory block pointed by the ptr pointer. See the following example:

Using the free() function

Memory is a limited resource therefore you should always deallocate or release the memory back when it is no longer used. To deallocate the memory that you have allocated, you use the free() function.

To use the free() function, you just need to pass a valid pointer to it. If you pass a NULL pointer, the free() function does nothing. If you accidentally call the free() function twice to deallocate the same memory block, the free() function will deallocate the memory block at the first call, and does nothing at the second call.

In this tutorial, we have introduced you to C dynamic memory allocation and how to use some handy functions in the C library function to allocate and deallocate memory.