Menu
Coddy logo textTech

Dynamic Allocation - malloc()

Part of the Logic & Flow section of Coddy's C journey — lesson 32 of 63.

Now you'll learn about malloc(), the primary function for requesting memory from the heap during program execution. The malloc() function stands for "memory allocation" and allows you to dynamically allocate memory when you need it, rather than declaring fixed-size variables at compile time.

The malloc() function takes one argument: the number of bytes you want to allocate. It returns a void pointer to the allocated memory block, or NULL if the allocation fails. Since it returns a void pointer, you can assign it directly to a typed pointer; casting is optional in C (unlike C++, which requires it).

#include <stdlib.h>

int *ptr = (int*)malloc(sizeof(int));  // Allocate memory for one integer
// ptr now points to a memory location that can hold an integer

The key advantage of malloc() is flexibility - you can request exactly the amount of memory you need at runtime, and this memory remains available until you explicitly free it. This is particularly useful when you don't know how much memory you'll need until your program is running.

Remember to include <stdlib.h> to use malloc(), casting the returned void pointer is optional in C, since it is automatically converted to the type you assign it to.

challenge icon

Challenge

Easy

Write a C program that demonstrates dynamic memory allocation using malloc() to create and work with a single integer on the heap. Your program should:

  1. Use malloc() to dynamically allocate memory for one integer
  2. Cast the returned void pointer to an integer pointer
  3. Read an integer value from the user input
  4. Store the input value in the dynamically allocated memory using the dereference operator
  5. Print the stored value in the format: Stored value: [value]
  6. Calculate and store the square of the value in the same memory location
  7. Print the squared value in the format: Squared value: [value]

Remember to include the <stdlib.h> header to use malloc(). Your program should demonstrate the basic workflow of dynamic memory allocation: requesting memory from the heap, using it to store and manipulate data, and working with the allocated memory through pointer operations.

For example, if the user enters 7, your output should be:

Stored value: 7
Squared value: 49

This challenge introduces you to the fundamental concept of dynamic memory allocation, where memory is requested at runtime rather than being fixed at compile time, giving your programs greater flexibility in memory usage.

Try it yourself

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

int main() {
    // Read input
    int input_value;
    scanf("%d", &input_value);
    
    // TODO: Write your code below
    // 1. Use malloc() to allocate memory for one integer
    // 2. Cast the returned pointer to int*
    // 3. Store the input value in the allocated memory
    // 4. Print the stored value
    // 5. Calculate and store the square in the same location
    // 6. Print the squared value
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow