I was just brushing up my C concepts a bit where I got confused about some behavior. Consider the following code snippet:
#include<stdio.h>
#include<stdlib.h>
int main (){
int * arr;
arr= malloc(3*sizeof(*arr));
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
printf("value is %d \n", arr[3]);
return 0;
}
The problem is that the program functions correctly! As far as I understand I allocate memory for an array of 3 integers. So basically when I try to put a value in arr[3] there should be a segmentation fault as no memory has been assigned for it. But it works fine and prints the value 4.
Either this is some weird behavior or I seriously need to revise basic C. Please if anyone can offer some explanation I would highly appreciate it.
Thanks.