Why do I get permission to read and write to unallocated memory?

I got confused in the behavior below the code snippet,

I declared a char pointer and pointed it to the allocated size memory cell (1 * sizeof (char)).

char *src ;
src = (char*)malloc(1 * sizeof(char));
strcpy(src,"Copy text");

Although I allocated only memory 1*sizeof(char), I can successfully copy the entire line, as well as get permission to read and write in the entire memory area where it is present "Copy text".

In the code below, the modified .ie value is printed, it prints "Copy RRxt".

src[5] = 'R';
src[6] = 'R';
printf("%s \n" , src);

So, I am confused why I did not get the error "Segmentation fault"in the above snippet.

Note. I am using the GCC v4.6.3 compiler.

+3
source share
1 answer

, malloc chunks of 16 or 32 bytes, . , 1 byte, next 15 bytes .

, , .

+15

All Articles