-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
53 lines (42 loc) · 1.45 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include"include/calloc.h"
#include"include/malloc.h"
#include"include/realloc.h"
#include"include/free.h"
/*
* Testing code to test all the functions implemented.
* First two new variables are created and allocated space using malloc()
* Then the first varaible is freed and malloc() is called again to show that space can be reused.
* Then calloc() is used to allocate an int array of size 5. Initially the values will be set to 0 by calloc();
* Then the array is resized to size 10 using realloc()
*/
int main(){
printf("\n\n");
printf("Initializing first variable : ");
int *a = (int*)malloc(sizeof(int));
*a = 5;
printf("%p -> %d \n\n",a,*a);
printf("Initializing second variable : ");
int *b = (int*)malloc(sizeof(int));
*b = 10;
printf("%p -> %d \n\n",b,*b);
free(a);
printf("Freeing first variable and using that memory block again using malloc() for third variable: ");
int *c = (int*)malloc(sizeof(int));
printf("%p -> %d \n\n",c,*c);
printf("Initializing int array of size 5 using calloc() : ");
int *arr = (int*)calloc(sizeof(int),5);
printf("%p -> ",arr);
for(int i=0; i<5; i++){
printf(" %d ", *(arr+i));
}
printf("\n\n");
printf("Resizing it to size 10 using realloc(): ");
arr = (int*)realloc(arr, 10);
printf("%p -> ",arr);
for(int i=0; i<10; i++){
printf(" %d ", *(arr+i));
}
printf("\n\n");
return 0;
}