0Pricing
C Academy · Lesson

Dynamic Memory Allocation

Explore functions like malloc, calloc, realloc, and free for managing memory dynamically.

Dynamic Memory Allocation is a free C Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Dynamic Memory Allocation

In C, dynamic memory allocation allows us to allocate memory at runtime instead of compile time.

In this lesson, you will learn:

  • How to allocate memory using malloc, calloc, and realloc.
  • How to free allocated memory using free.
  • How to avoid memory leaks.
Dynamic Memory Allocation — illustration 1

2

What is Dynamic Memory Allocation?

Normally, memory for variables is allocated at compile time.

With dynamic memory allocation, memory is allocated during program execution using special functions.

This is useful for managing arrays or structures of unknown size at compile time.

3

Allocating Memory with malloc()

The malloc function allocates memory but does not initialize it.

Syntax:

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

It returns a pointer to the allocated memory or NULL if allocation fails.

4

Example: Using malloc()

This program demonstrates how to allocate memory using malloc.

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

int main() {
    int *ptr = (int*) malloc(5 * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        ptr[i] = i * 10;
        printf("%d ", ptr[i]);
    }
    free(ptr);
    return 0;
}

5

Allocating Memory with calloc()

The calloc function allocates memory and initializes it to zero.

Syntax:

ptr = (int*) calloc(num_elements, sizeof(int));

It returns a pointer to the allocated memory or NULL if allocation fails.

6

Example: Using calloc()

This program demonstrates how to allocate memory using calloc.

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

int main() {
    int *ptr = (int*) calloc(5, sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d ", ptr[i]); // All elements initialized to 0
    }
    free(ptr);
    return 0;
}

7

Reallocating Memory with realloc()

The realloc function changes the size of previously allocated memory.

Syntax:

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

It returns a pointer to the new memory block.

8

9

Freeing Memory with free()

Allocated memory should be freed using free() to avoid memory leaks.

Syntax:

free(ptr);

Once freed, the pointer should not be accessed.

10

Summary

In this lesson, you learned:

  • How to allocate memory using malloc and calloc.
  • How to reallocate memory using realloc.
  • How to free memory using free to prevent memory leaks.

Next, we will explore pointer arrays and function pointers in C!

Dynamic Memory Allocation — illustration 10

Frequently asked questions

Is the “Dynamic Memory Allocation” lesson free?

Yes — the full text of “Dynamic Memory Allocation” is free to read here on the web, and the C Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Dynamic Memory Allocation”?

Explore functions like malloc, calloc, realloc, and free for managing memory dynamically. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Dynamic Memory Allocation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Pointers
  2. Dynamic Memory Allocation
  3. Pointer Arrays and Function Pointers
← Back to C Academy