Take this contrived example:
#include <stdlib.h>
#include <string.h>
int main (int argc, char const *argv[])
{
char *buf = malloc(8);
strcpy(buf,"Hello W");
char *last = &buf[4];
size_t u = *(size_t *)(last);
printf("0x%lx",u); // prints "0x57206f" on little endian
return 0;
}
As per my (rather basic) understanding of C's memory management this would result in the following memory read (assuming 64 Bit):
+--+--+--+--+--+--+--+--+--+--+--+--+--+
|H |e |l |l |o | |W |\0|? |? |? |? |… |
+--+--+--+--+--+--+--+--+--+--+--+--+--+
^^^^^^^^^^^^^^^^^^^^^^^
Thereby accessing a memory region that might have not been allocated to the program and causing a crash. However this seems to work fine in practice – is this defined behavior?
Addition:
I made this example from the code shown here: http://www.daemonology.net/blog/2008-06-05-faster-utf8-strlen.html