Test Case: I want to allocate memory using realloc() on an int array. I allocate memory for 2 values, but if I write the third value and so on to the array, they are also written and displayed. Why am I not getting a SegFault error?
P.s Exactly the same problem when using malloc()
#include <stdio.h>
#include <stdlib.h>
typedef struct Test
{
int *nums;
} Test;
int main()
{
Test test;
test.nums = NULL;
test.nums = realloc(test.nums, 2 * sizeof(int));
test.nums[0] = 1;
test.nums[1] = 2;
test.nums[2] = 3;
test.nums[3] = 4;
test.nums[4] = 5;
printf("%d %d %d %d %d\n", test.nums[0], test.nums[1], test.nums[2], test.nums[3], test.nums[4]);
free(test.nums);
return 0;
}