Home »
Code Examples »
C Code Examples
C - Use of free() function using malloc() Code Example
The code for Use of free() function using malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n = 25;
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Chceck whether memory is allocated or not
if (ptr == NULL) {
printf("Memory not allocated \n");
exit(0);
}
printf("Memory allocation is done.\n");
// Freeing the memory using free() function
free(ptr);
printf("Memory freed.");
return 0;
}
/*
Output:
Memory allocation is done.
Memory freed.
*/
Code by IncludeHelp,
on August 11, 2022 19:58