C Program to Free Allocated Memory Using free()

Introduction

Dynamic memory allocation in C allows you to allocate memory at runtime using functions like malloc(), calloc(), or realloc(). After you have finished using the allocated memory, it is crucial to free it using the free() function. Failing to free dynamically allocated memory can lead to memory leaks, which occur when a program consumes more memory than it needs, eventually exhausting the available memory.

Example:

  • Input: Allocate memory for an array of integers.
  • Output: Free the allocated memory after use.

Problem Statement

Create a C program that:

  • Allocates memory dynamically using malloc() or calloc().
  • Uses the allocated memory for some operations.
  • Frees the allocated memory using free().

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> for standard input-output functions and #include <stdlib.h> for dynamic memory allocation functions.
  2. Allocate Memory Dynamically Using malloc() or calloc(): Allocate memory for an array or any other data structure.
  3. Perform Operations on the Allocated Memory: Use the allocated memory for any purpose, such as storing and displaying data.
  4. Free the Allocated Memory Using free(): Use the free() function to deallocate the memory once it is no longer needed.

C Program to Free Allocated Memory Using free()

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

int main() {
    int *ptr;
    int n, i;

    // Step 2: Allocate memory dynamically using malloc()
    printf("Enter the number of elements: ");
    scanf("%d", &n);

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

    // Check if memory allocation was successful
    if (ptr == NULL) {
        printf("Memory allocation failed.\n");
        return 1;  // Exit the program if memory allocation fails
    }

    // Step 3: Perform operations on the allocated memory
    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &ptr[i]);
    }

    printf("The elements you entered are:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", ptr[i]);
    }
    printf("\n");

    // Step 4: Free the allocated memory
    free(ptr);
    printf("Memory successfully freed.\n");

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Allocate Memory Dynamically Using malloc()

  • The program asks the user to input the number of elements they want to store, which is stored in the variable n.
  • The malloc() function is used to allocate memory for n integers. The size of memory allocated is n * sizeof(int).
  • The pointer ptr is used to point to the allocated memory.

Step 3: Perform Operations on the Allocated Memory

  • A for loop is used to input the elements from the user and store them in the dynamically allocated memory.
  • Another for loop is used to display the elements stored in the allocated memory.

Step 4: Free the Allocated Memory

  • The free() function is called to deallocate the memory allocated by malloc(). This ensures that the memory is returned to the system and prevents memory leaks.
  • After freeing the memory, a message is printed to confirm that the memory has been successfully freed.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the number of elements: 5
Enter 5 elements:
1 2 3 4 5
The elements you entered are:
1 2 3 4 5
Memory successfully freed.

Conclusion

This C program demonstrates how to allocate memory dynamically using malloc() and how to free that memory using free(). It covers basic concepts such as dynamic memory allocation, pointer manipulation, and memory deallocation, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top