-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.c
57 lines (36 loc) · 1.73 KB
/
24.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
54
55
56
57
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
// Dynamic Memory (Heap) allocation
// Using realloc() to change the size of a previously allocated Dynamic Memory block
int n;
printf("Enter the desired size of the array of integers: ");
scanf("%d", &n);
// Allocating a block of Dynamic Memory (Heap) for the desired array of integers
int *A = (int *) calloc(n, sizeof(int));
// Filling in some data into the dynamically allocated array
for (int i = 0; i < n; i++){
A[i] = i + 1;
}
// We will realloc the previous block, doubling its size
// It'll create a new memory block of size 2n, and copy the values in the previous A into the new one
// If the size of the new block > the size of the old block:
// If it's possible to find consecutive memory to just extend the old one:
// The previous block itself is extended.
// Else:
// A new block is allocated, the content of the old one is copied, and then it's deallocated
// Else:
// The same block will be reduced
int *B = (int *) realloc(A, 2 * n * sizeof(int));
// Printing the address of the old and the new block
printf("\nThe address of the previous Heap block is: %p.\n", A);
printf("The address of the new Heap block is: %p.\n", B);
// We can see that it was possible to only extend the existent block
// Printing all the elements in the array
printf("\n==========ALL ELEMENTS IN THE ARRAY==========\n");
for (int i = 0; i < 2*n; i++){
printf("B[%d] (or *(B + %d)) = %d (or %d)\n", i, i, *(B + i), B[i]);
}
// All elements after the nth element (including it) have garbage values
return 0;
}