-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
72 lines (58 loc) · 2.15 KB
/
realloc.c
File metadata and controls
72 lines (58 loc) · 2.15 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num_elems;
printf("\nHow many elements do you want to enter? ");
scanf("%d", &num_elems);
printf("\n\n******************First Malloc **************************************");
// If malloc is not able to allot - you will get a segmentation fault
int *imallocarray1 = (int *)malloc(num_elems * sizeof(int) );
if (imallocarray1 == NULL)
{
printf("\n%d bytes could not be allotted by the first malloc", num_elems * sizeof(int) );
}
else
{
printf("\n%d bytes allotted", num_elems * sizeof(int) );
}
//for (int i = 0; i < num_elems; i++) imallocarray1[i] = 2*i;
for (int i = 0; i < num_elems; i++)
{
printf("\n%d is the %dth element at address %p", imallocarray1[i], i, &imallocarray1[i]);
}
printf("\n\n********************Second Malloc************************************");
int *imallocarray2 = (int *)malloc(num_elems * sizeof(int) );
if (imallocarray2 == NULL)
{
printf("\n%d bytes could not be allotted by the second malloc", num_elems);
}
else
{
printf("\n%d bytes allotted", num_elems * sizeof(int) );
}
//for (int i = 0; i < num_elems; i++) imallocarray2[i] = 3*i;
for (int i = 0; i < num_elems; i++)
{
printf("\n%d is the %dth element at address %p", imallocarray2[i], i, &imallocarray2[i]);
}
printf("\n\n*************************Realloc Usage*******************************");
printf("\nReallocating using the first malloc pointer");
int *ireallocarray2 = (int *)realloc(imallocarray1, sizeof(int) * 2*num_elems);
if (ireallocarray2 == NULL)
{
printf( "\n\n%d bytes could not be allotted by realloc", sizeof(int) * 2*num_elems);
}
else
{
printf("\n%d bytes allotted", 2*num_elems * sizeof(int));
}
for (int i = 0; i < 2*num_elems; i++)
{
printf("\n%d is the %dth element at address %p", ireallocarray2[i], i, &ireallocarray2[i]);
}
printf("\n\n****Reallocating using compile time array pointer- FAIL Case - ABORTED CORE DUMP RunTime Error***");
printf("\n\n****REALLOC - FAIL Case - *********************\n");
int arr[10];
int *iarray = (int *)realloc(arr, 25);
}