How to Release Memory in C? Last Updated : 17 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In C programming, releasing memory means deallocating memory that was previously allocated dynamically using functions like malloc(), calloc(), or realloc(). In this article, we will learn how to release memory in C. Free Dynamic Memory in CTo release the dynamically allocated memory, we can use the free() function in C. The free function takes a pointer as an argument that points to the memory block allocated by the malloc(), calloc(), or realloc() functions and deallocates the memory making it available for reuse. Syntax of free()free(ptr);here, ptr is the pointer that points to the dynamically allocated memory block.Note: Static memory (i.e. memory allocated on the stack) cannot be deleted by the user. C Program to Release Allocated MemoryThe following program illustrates how we can release memory in C using free() function: C // C Program to release dynamically allocated memory #include <stdio.h> #include <stdlib.h> int main() { /* ____ Release memory for an integer ______ */ // Allocate memory for a single integer int* ptr_single = (int*)malloc(sizeof(int)); if (ptr_single == NULL) { printf("Memory allocation failed\n"); return 1; } // Use the allocated memory for a single variable *ptr_single = 42; printf("Value of variable: %d\n", *ptr_single); // Release the memory allocated for the single variable free(ptr_single); printf("Memory for variable released\n"); // Set the pointer to NULL to avoid dangling pointers ptr_single = NULL; /* ____ Release memory for a dynamically allocated array * ______ */ // Allocate memory for an array of integers int* ptr_array = (int*)malloc(5 * sizeof(int)); if (ptr_array == NULL) { printf("Memory allocation failed\n"); return 1; } // Use the allocated memory for an array for (int i = 0; i < 5; i++) { ptr_array[i] = i; } printf("Array elements:"); for (int i = 0; i < 5; i++) { printf("%d ", ptr_array[i]); } // Release the memory allocated for the array free(ptr_array); printf("\nMemory for array released\n"); // Set the pointer to NULL to avoid dangling pointers ptr_array = NULL; return 0; } OutputValue of variable: 42 Memory for variable released Array elements:0 1 2 3 4 Memory for array released Time Complexity: O(1), here denotes the number of elements in the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Release Memory in C? A am8254s3a Follow Improve Article Tags : C Programs C Language C-Dynamic Memory Allocation C Examples Similar Reads How to Detect Memory Leaks in C? In C, memory leaks occur when programmers allocate memory by using functions like malloc, calloc, realloc etc., but forget to deallocate the memory by using free function. In this article, we will learn how to detect memory leaks in C. Detecting Memory Leaks in C ProgramsDetecting memory leaks in C 3 min read How to Initialize Array of Pointers in C? Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe 2 min read How to Find Size of Dynamic Array in C? In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. Itâs particularly useful when dealing with arrays where the size isnât known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o 2 min read Static and Dynamic Memory Allocation in C Memory is divided into smaller addressable units called bytes. Assume that these are small boxes as bytes. Each byte has its own address as per the below table.For example: 0, 1, 2, 3, 4, 5, 6, etc. How program uses memory? The Memory is divided into three sections. Heap Memory: It is a part of the 8 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read C Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Return an Array in C In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C 5 min read How to Release Memory in C++? In C++, releasing memory means deallocating the memory that was previously allocated by the user. It is very important to avoid memory leaks. Other programming languages like Java support automatic garbage collection to release the dynamically allocated memory, but in C++ we have to release the allo 2 min read How to Detect Memory Leaks in C++? Memory leaks are a common and serious problem in C++ programming. They occur when dynamically allocated memory using operators like new or functions like malloc from the heap is not properly deallocated using delete or free, resulting in slow system resource utilization, degraded performance, and po 4 min read Like