Unfortunately, the action of overwriting a buffer does not generate a Segmentation Violation. If you happen to overwrite a buffer and trample on some data that your program expects to use at a later time, then this would probably lead to a Segmentation Violation. This is why troubleshooting these types of problems can be tricky.
Originally Posted by allynm
Here's a crude example that demonstrates how a program continues running:
Btw, with newer compilers, there is no need to cast the return value of malloc() as dazman19 indicated.
char* ptr1 = malloc(10);
char* ptr2 = malloc(10);
printf("ptr1 is pointing to: %p\n", ptr1);
printf("ptr2 is pointing to: %p\n", ptr2);
strcpy(ptr2, "Hello World\n");
memset(ptr1, 'J', ptr2-ptr1 + 1); // overwrite buffer with 'J' chars
printf("data at ptr2 is: %s\n", ptr2);
// These are commented out because a SegFault will occur once called.