This code writes additional data by increasing the size by 3 when it reaches the size (cur) allocated to the array after dynamically allocating 5 arrays.
The code I wrote is below.
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int i = 0;
int* arr = (int*)malloc(sizeof(int) * 5);
int curs = sizeof(arr) * 5 / sizeof(int);
while (1) {
printf("Input Num : (-1 input exit): ");
scanf("%d", &arr[i]);
if (arr[i] == -1)
break;
if ((i + 1) == curs) {
arr = (int*)realloc(arr, sizeof(int) * (3 + curs));
printf("Add Allocation Arr!\n");
curs += 3;
}
i++;
}
printf(" Arr : ");
for (int j = 0; j < i; j++) {
printf("%d ", arr[j]);
}
free(arr);
return 0;
}
When I entered the data as below, it worked fine.
Input Data(Input -1 Exit): 1
Input Data(Input -1 Exit): 2
Input Data(Input -1 Exit): 3
Input Data(Input -1 Exit): 4
Input Data(Input -1 Exit): 5
Add Allocation Arr!
Input Data(Input -1 Exit): 1
Input Data(Input -1 Exit): 2
Input Data(Input -1 Exit): 3
Add Allocation Arr!
Input Data(Input -1 Exit): -1
Arr: 1 2 3 4 5 1 2 3
Here, a friend tried one thing.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int* arr = (int*)malloc(sizeof(int) * 5);
int i = 0;
int curs = sizeof(arr) * 5 / sizeof(int);
while (1) {
printf("Input Data(Input -1 Exit): ");
scanf("%d", &arr[i]);
if (arr[i] != -1) {
if ((i + 1) == curs) {
printf("Add Allocation Arr!\n");
curs += 3;
i++; // ★<-- don't use realloc, just i++
continue;
}
else {
i++;
continue;
}
}
else {
printf("Arr: ");
for (int j = 0; j < i; j++) {
printf("%d ", arr[j]);
}
break;
}
}
free(arr);
return 0;
}
If you look at the ★ in the comment above, you can see that the array is accessed by simply increasing i without realloc.
When I entered the data as below, it worked fine.
Input Data(Input -1 Exit): 1
Input Data(Input -1 Exit): 2
Input Data(Input -1 Exit): 3
Input Data(Input -1 Exit): 4
Input Data(Input -1 Exit): 5
Add Allocation Arr!
Input Data(Input -1 Exit): 1
Input Data(Input -1 Exit): 2
Input Data(Input -1 Exit): 3
Add Allocation Arr!
Input Data(Input -1 Exit): 1
Input Data(Input -1 Exit): 2
Input Data(Input -1 Exit): 3
Add Allocation Arr!
Input Data(Input -1 Exit): -1
Arr: 1 2 3 4 5 1 2 3 1 2 3
Why is this running?
I was curious, and I used the code below to check the memory status.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int* arr = (int*)malloc(sizeof(int) * 5);
int i = 0;
int curs = sizeof(arr) * 5 / sizeof(int);
for (i = 0; i < 20; i++)
{
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
The execution result is as follows.
-842150451 -842150451 -842150451 -842150451 -842150451 -33686019 7077988 108 -969574923 134254852 12127432 12077032 12127440 12077040 12127448 12077048 2076049408 2076489216 1527808 4456514
I wanted to access it using only the 5 allocated memory. It was expected that an error would occur after accessing the arr[4] index, but that result was printed. It's very confusing.