I'm confused on the behavior of below code snippet,
I have declared a char pointer and pointed it to a memory location of allocated size (1 * sizeof(char)) .
char *src ;
src = (char*)malloc(1 * sizeof(char));
strcpy(src,"Copy text");
Even though I have only allocated memory of 1*sizeof(char)
I can succesfully copy the entire string and also I'm getting read and write permission on the entire memory area where "Copy text"
is present .
i.e the below code prints the modified value.ie, it prints "Copy RRxt"
.
src[5] = 'R';
src[6] = 'R';
printf("%s \n" , src);
So I'm confused why I didn't get "Segmentation fault"
error on the above snippet.
Note: I'm using GCC compiler v4.6.3