#include <stdio.h>
#include <stdlib.h>
#define SIZE_1 5
#define SIZE_2 10
void fill_array(int *array, int init, int end) {
for (int i= init; i < end; i++) {
array[i] = i;
}
}
void display_array(int *array, int init, int end) {
for (int i= init; i < end; i++) {
printf("array[%d]=%d\n", i, array[i]);
}
printf("\n");
}
int main() {
int *ptr = (int*) calloc(SIZE_1, sizeof(int));
printf("The address of ptr is %p\n", ptr);
fill_array(ptr, 0, SIZE_1);
display_array(ptr, 0, SIZE_1);
ptr = (int*) realloc(ptr, SIZE_2 * sizeof(int));
printf("The address of ptr is %p\n", ptr);
fill_array(ptr, SIZE_1, SIZE_2);
display_array(ptr, 0, SIZE_2);
free(ptr);
return 0;
}
3. Dynamic memory functions - realloc
Systems Architecture - 6. Dynamic memory in C 23
The address of ptr is 0x560b3f37e2a0
stack (main)
ptr
heap