Back to C Programming
2026-07-155 min read

Example 1: malloc() and free()

Learn Example 1: malloc() and free() step by step with clear examples and exercises.

Why This Matters

Dynamic memory allocation plays a crucial role in C programming as it allows developers to manage memory during runtime, providing flexibility when working with data structures of unknown size at compile time. Understanding and effectively using malloc(), calloc(), free(), and realloc() is essential for writing efficient and robust C programs.

Prerequisites

Before diving into this lesson, you should have a good understanding of the following topics:

  • Basic C programming concepts such as variables, data types, and operators
  • Control structures like loops and conditional statements
  • Understanding pointers in C
  • Familiarity with basic file I/O operations using stdio.h

Core Concept

Allocating Memory with malloc()

The malloc() function is used to dynamically allocate memory. It takes one argument: the number of bytes you want to allocate. Here's a simple example:

int *ptr;
ptr = (int *)malloc(5 * sizeof(int));

In this code, we first declare a pointer ptr that can hold an integer value. Then, we use malloc() to allocate memory for 5 integers. The sizeof(int) ensures the correct number of bytes are allocated.

Important Notes:

  • Always cast the return value of malloc() to avoid compiler warnings.
  • If malloc() fails to allocate memory, it returns a null pointer.

Freeing Memory with free()

After using memory, it's essential to free it up when no longer needed to prevent memory leaks. This is where the free() function comes in:

free(ptr);

In this example, we pass our pointer ptr to the free() function to release the allocated memory.

Allocating and Initializing Memory with calloc()

The calloc() function is similar to malloc(), but it also initializes the memory to zero:

int *arr = calloc(5, sizeof(int));

In this code, we allocate memory for 5 integers and initialize them all to zero.

Resizing Memory with realloc()

The realloc() function allows us to change the size of an already allocated block of memory:

ptr = realloc(ptr, 10 * sizeof(int));

In this example, we double the size of our previously allocated memory for integers. If the new memory cannot be allocated, realloc() returns a null pointer.

Important Notes:

  • When using realloc(), the contents of the original memory block are copied to the new one if successful.
  • If realloc() fails to allocate memory, it returns a null pointer.

Worked Example

Let's create a simple program that dynamically allocates memory for an array, initializes it with some values using calloc(), prints the contents of the array, and then frees the memory:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr;
int n = 5;

// Allocate memory for an array of 5 integers and initialize it with values
arr = calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

// Initialize the array with even numbers
for (int i = 0; i < n; ++i) {
arr[i] = 2 * i;
}

// Print the contents of the array
printf("Array before free():\n");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");

// Free the allocated memory
free(arr);

// Print the contents of the array after freeing it (should cause a segmentation fault)
printf("Array after free():\n");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

When you run this program, it will output:

Array before free():
0 2 4 6 8
Array after free():
Segmentation fault (core dumped)

Common Mistakes

  1. Forgetting to include the necessary header files: Always remember to include ` for input/output operations and ` for dynamic memory allocation functions.
  2. Not checking for a null pointer returned by malloc() or calloc(): If memory allocation fails, these functions return a null pointer. Always check for this to handle errors gracefully.
  3. Forgetting to free allocated memory: Failing to free allocated memory can lead to memory leaks. Make sure you always free the memory when it's no longer needed.
  4. Using free() on a null pointer: If you pass a null pointer to free(), it does nothing and may cause undefined behavior. Always check for a null pointer before calling free().
  5. Not initializing memory with calloc(): When using calloc(), the memory is initialized to zero, so if you don't initialize your data explicitly, you might end up with uninitialized values.
  6. Forgetting to handle errors when using realloc(): If realloc() fails to allocate memory, it returns a null pointer. Make sure to check for this and handle the error gracefully.
  7. Not considering the null terminator when working with character arrays: When allocating memory for a character array, don't forget to include space for the null terminator (\0) in your calculations.

Practice Questions

  1. Write a program that dynamically allocates memory for 10 floating-point numbers and initializes them with random values using calloc(). Then, print the contents of the array and free the allocated memory.
  2. Modify the previous example to use malloc() instead of calloc(). How does the output change when you run the program?
  3. Write a program that uses realloc() to double the size of an array of integers and then prints the contents of the array. If the memory cannot be reallocated, handle the error gracefully.
  4. Write a function that takes an integer array and its length as arguments, dynamically allocates memory for a new array with double the capacity, copies the original array elements into the new one, and frees the old memory.
  5. Write a program that reads a line of text from standard input, dynamically allocates memory to store the line, and then frees the allocated memory after printing it.

FAQ

  1. Why should I use dynamic memory allocation instead of arrays? Dynamic memory allocation allows us to allocate memory during runtime based on our needs, providing flexibility when working with data structures that may have an unknown size at compile time.
  2. What happens if I don't free the allocated memory in C? Failing to free allocated memory can lead to memory leaks, which can cause your program to consume more memory than necessary and potentially crash due to lack of available memory.
  3. Can I use malloc() to allocate memory for a character array? Yes, you can use malloc() to allocate memory for a character array. Just make sure to include the correct number of bytes, including space for the null terminator (\0).
  4. What is the difference between calloc() and malloc()? Both calloc() and malloc() are used to dynamically allocate memory in C. The main difference is that calloc() initializes the allocated memory to zero, while malloc() does not.
  5. Why do I get a segmentation fault when I try to access an array after freeing its memory? When you call free(ptr), the memory pointed by ptr is deallocated and becomes available for reuse. Accessing that memory afterwards can cause a segmentation fault because it no longer belongs to your program.
  6. What are some common mistakes when using dynamic memory allocation in C? Common mistakes include forgetting to include necessary header files, not checking for null pointers returned by malloc() or calloc(), forgetting to free allocated memory, using free() on a null pointer, not initializing memory with calloc(), and forgetting to handle errors when using realloc().