Further to Mehrdads answer, In gcc with glibc, the data structures representing the memory heap are stored in the block of memory returned, to save memory (i.e. its an intrusive list). So, when a block of memory is freed, it is added to the free list. My guess is that the 0 being written following the free indicates that this is the last element of the the free block list (the first pointer sized word of deallocated memory will contain the lists next
pointer).
If you were to allocate and free more memory before dereferencing this block again, the value would change when a new item is added to the end of the free list. This is one way that library implementation decisions affect what happens during 'undefined' behaviour. In this case, the glibc developers have taken advantage of the fact that this behaviour is undefined to make their memory allocator more space efficient.
If you run your program under valgrind, it will catch these errors for you. In any case, always stay away from undefined behaviour, since it is very likely to be different on various platforms and even different builds on the same platform (e.g. debug vs release).