Common Errors During Dynamic Memory Allocation in C

What is Dynamic Memory Allocation in C?

Dynamic memory allocation refers to the process of allocating memory manually during runtime using the following standard library functions:

Function Purpose
malloc() Allocates a block of memory (uninitialized).
calloc() Allocates and initializes memory to zero.
realloc() Resizes previously allocated memory block.
free() Frees previously allocated memory.

Common Errors During Dynamic Memory Allocation in C

1. Not Checking for NULL After Allocation

When memory allocation fails (e.g., due to insufficient memory), functions like malloc() return NULL. If you blindly use the pointer without checking, it may lead to a segmentation fault.

Incorrect Code:

int *score = (int *)malloc(5 * sizeof(int));
score[0] = 10;  // May crash if malloc fails

Correct Code:

int *score = (int *)malloc(5 * sizeof(int));
if (score == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}
score[0] = 10;

2. Memory Leak (Forgetting to free() Memory)

advertisement

If you allocate memory and don’t release it using free(), your program may consume more memory over time, leading to a memory leak.

Incorrect Code:

int *quiz = (int *)malloc(5 * sizeof(int));
// ... used but never freed

Correct Code:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
int *quiz = (int *)malloc(5 * sizeof(int));
// ... use quiz
free(quiz);  // Prevents memory leak

3. Dangling Pointer (Access After Free)

Accessing memory after it has been freed leads to undefined behavior and often a crash.

Incorrect Code:

int *data = (int *)malloc(3 * sizeof(int));
free(data);
printf("%d", data[0]);  // Dangling pointer access

Fix:

int *data = (int *)malloc(3 * sizeof(int));
free(data);
data = NULL;  // Safe: avoids accidental access

4. Double Free

Freeing the same memory twice can cause undefined behavior, crashes, or vulnerabilities.

advertisement

Incorrect Code:

int *marks = (int *)malloc(4 * sizeof(int));
free(marks);
free(marks);  // Error: double free

Fix:

int *marks = (int *)malloc(4 * sizeof(int));
free(marks);
marks = NULL;  // Safe: avoids accidental access

5. Memory Leak due to Reassignment

Reassigning a pointer before freeing it causes a memory leak.

int *list = (int *)malloc(5 * sizeof(int));
list = (int *)malloc(10 * sizeof(int));  // First block is leaked

Fix:

int *list = (int *)malloc(5 * sizeof(int));
free(list);  // Free before reassigning
list = (int *)malloc(10 * sizeof(int));

6. Improper malloc() Size Calculation

Using incorrect size for data type causes insufficient or excessive allocation.

int *arr = (int *)malloc(10);  // May not allocate enough for 10 integers

Correct:

int *arr = (int *)malloc(10 * sizeof(int));

7. Accessing Uninitialized Memory

Using memory before assigning values leads to garbage data or bugs.

int *quiz = (int *)malloc(3 * sizeof(int));
printf("%d", quiz[0]);  // Undefined value

Correct:

int *quiz = (int *)malloc(3 * sizeof(int));
if (quiz == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}
 
// Initialize memory before use
for (int i = 0; i < 3; i++) {
    quiz[i] = 0;
}
 
printf("%d", quiz[0]);  // Now safe and predictable
free(quiz);

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.