I am simply testing what would be the output if I try to dereference a pointer, which points to out of range of dynamically created memory using calloc()
and expecting memory fault or some garbage value instead. It is giving 0 as output, which is basic feature of calloc()
that it intializes allocated memory with '0', or is it just due to undefined behavior?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = NULL;
p = (int *)calloc(10, 8);
if(p != NULL)
{
printf("\n successfully allocated memory ");
}
else
EXIT_FAILURE;
printf("\n Thirty first element of memory is %d \n",p[30]);
}
Result :
successfully allocated memory
Thirty first element of memory is 0