Home›Learn›C›Dynamic Memory Allocation
C PROGRAMMING MASTER COURSEBEGINNER FRIENDLY

Dynamic Memory Allocation

Learn Dynamic Memory Allocation with simple explanations, practical examples, coding practice, MCQs and interview preparation.

📖 Chapter 18 / 22⏱️ 8–10 Min Read🎯 Beginner Friendly
Course Progress82%
📘

Theory

Understand the concept step by step

Dynamic Memory Allocation in C

Dynamic memory allocation allows programs to request memory during runtime.

Important functions are:

  • malloc()
  • calloc()
  • realloc()
  • free()

These functions are declared in stdlib.h.

malloc()

Allocates a block of memory.

calloc()

Allocates memory for multiple elements and initializes the allocated bytes to zero.

realloc()

Changes the size of a previously allocated block.

free()

Releases dynamically allocated memory.

💻

Example

Try this C program yourself

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

int main() {
    int *numbers = malloc(3 * sizeof(int));

    if (numbers == NULL) {
        return 1;
    }

    numbers[0] = 10;
    numbers[1] = 20;
    numbers[2] = 30;

    printf("%d %d %d",
           numbers[0],
           numbers[1],
           numbers[2]);

    free(numbers);

    return 0;
}
📤

Output

Expected result of the program

Output
10 20 30
📝

Practice

Test your understanding

💡

Try it yourself

Don't just read the example. Open your C editor and write the program yourself. Try changing the values and observe what happens.

PRACTICE 01

Modify the example program and experiment with different values.

PRACTICE 02

Write the same program from scratch without looking at the example.

❓

Quick Quiz

Test what you learned

Question 1 of 5Score: 0

What type of language is C?

💼

Interview Questions

Prepare for your C programming interview

1

Interview Question

What is C programming language?

+

Answer

C is a general-purpose programming language widely used for system programming, embedded systems, operating systems and application development.

2

Interview Question

Why is C still important?

+

Answer

C provides efficient low-level memory access and is widely used in systems programming, embedded systems and performance-critical software.

3

Interview Question

What is the purpose of main() in C?

+

Answer

The main() function is the designated entry point of a hosted C program. Program execution begins by calling main().

Chapter 18 of 22

Keep going — you're doing great! 🚀

82%