-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ee1a21
commit cdebae8
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() | ||
{ | ||
|
||
// This pointer will hold the | ||
// base address of the block created | ||
int *ptr, *ptr1; | ||
int n, i; | ||
|
||
// Get the number of elements for the array | ||
n = 5; | ||
printf("Enter number of elements: %d\n", n); | ||
|
||
// Dynamically allocate memory using malloc() | ||
ptr = (int*)malloc(n * sizeof(int)); | ||
|
||
// Dynamically allocate memory using calloc() | ||
ptr1 = (int*)calloc(n, sizeof(int)); | ||
|
||
// Check if the memory has been successfully | ||
// allocated by malloc or not | ||
if (ptr == NULL || ptr1 == NULL) { | ||
printf("Memory not allocated.\n"); | ||
exit(0); | ||
} | ||
else { | ||
|
||
// Memory has been successfully allocated | ||
printf("Memory successfully allocated using malloc.\n"); | ||
|
||
// Free the memory | ||
free(ptr); | ||
printf("Malloc Memory successfully freed.\n"); | ||
|
||
// Memory has been successfully allocated | ||
printf("\nMemory successfully allocated using calloc.\n"); | ||
|
||
// Free the memory | ||
free(ptr1); | ||
printf("Calloc Memory successfully freed.\n"); | ||
} | ||
|
||
return 0; | ||
} |