C malloc and free Tutorial
last modified January 29, 2024
Dynamic memory allocation in C is a powerful feature that allows programs to
allocate memory at runtime. The malloc function is used to allocate
memory, and the free function is used to deallocate it. This
tutorial covers the basics of malloc and free, their
usage, and practical examples.
What Are malloc and free?
The malloc function allocates a block of memory of a specified size
and returns a pointer to the beginning of the block. The free
function deallocates the memory previously allocated by malloc,
making it available for future allocations. Proper use of these functions is
crucial to avoid memory leaks and undefined behavior.
Basic Memory Allocation
This example demonstrates how to allocate and deallocate memory using
malloc and free.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1; // Assign values to the allocated memory
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]); // Print the values
}
free(ptr); // Deallocate the memory
return 0;
}
The malloc function allocates memory for 5 integers, and the
free function deallocates the memory. Always check if
malloc returns NULL to handle allocation failures.
Allocating Memory for a String
This example demonstrates how to allocate memory for a string and deallocate it using free.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str;
str = (char *)malloc(50 * sizeof(char)); // Allocate memory for a string
if (str == NULL) {
printf("Memory allocation failed\n");
return 1;
}
strcpy(str, "Hello there!"); // Copy a string into the allocated memory
printf("%s\n", str); // Print the string
free(str); // Deallocate the memory
return 0;
}
The malloc function allocates memory for a string, and the
free function deallocates it. The strcpy function is
used to copy a string into the allocated memory.
Reallocating Memory
This example demonstrates how to reallocate memory using the
realloc function.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1; // Assign values to the allocated memory
}
ptr = (int *)realloc(ptr, 10 * sizeof(int)); // Reallocate memory for 10 integers
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
for (int i = 5; i < 10; i++) {
ptr[i] = i + 1; // Assign values to the reallocated memory
}
for (int i = 0; i < 10; i++) {
printf("%d ", ptr[i]); // Print the values
}
free(ptr); // Deallocate the memory
return 0;
}
The realloc function is used to resize the previously allocated
memory block. It can expand or shrink the memory block as needed.
Allocating Memory for a 2D Array
This example demonstrates how to allocate memory for a 2D array using
malloc.
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
int **arr;
// Allocate memory for rows
arr = (int **)malloc(rows * sizeof(int *));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Allocate memory for columns
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int));
if (arr[i] == NULL) {
printf("Memory allocation failed\n");
return 1;
}
}
// Assign values to the 2D array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = i * cols + j + 1;
}
}
// Print the 2D array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
// Deallocate memory
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
The malloc function is used to allocate memory for a 2D array.
Memory is allocated for each row and column separately, and free is
used to deallocate the memory.
Handling Memory Leaks
This example demonstrates how to avoid memory leaks by properly deallocating memory.
#include#include int main() { int *ptr; ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers if (ptr == NULL) { printf("Memory allocation failed\n"); return 1; } for (int i = 0; i < 5; i++) { ptr[i] = i + 1; // Assign values to the allocated memory } // Memory leak: Forgot to free the allocated memory // free(ptr); return 0; }
Forgetting to call free results in a memory leak, where the
allocated memory is not deallocated. Always ensure that every
malloc call is paired with a corresponding free call.
Using calloc for Zero-Initialized Memory
This example demonstrates how to use calloc to allocate
zero-initialized memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)calloc(5, sizeof(int)); // Allocate and zero-initialize memory for 5 integers
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]); // Print the zero-initialized values
}
free(ptr); // Deallocate the memory
return 0;
}
The calloc function allocates memory and initializes it to zero. It
is useful when you need zero-initialized memory.
Best Practices for Using malloc and free
- Check for NULL: Always check if
mallocorcallocreturnsNULLto handle allocation failures. - Pair malloc with free: Ensure that every
malloccall is paired with a correspondingfreecall to avoid memory leaks. - Use calloc for Zero-Initialization: Use
callocwhen you need zero-initialized memory. - Avoid Dangling Pointers: Set pointers to
NULLafter callingfreeto avoid dangling pointers.
Source
In this article, we have explored the use of malloc and free in C and demonstrated their usage through practical examples.
Author
List all C tutorials.