In response to Mehrdads answer: In gcc with glibc, data structures representing a bunch of memory are stored in the returned memory block to save memory (i.e. its obsessive list). Thus, when a block of memory is freed, it is added to the free list. My guess is that the 0, written after the free one, indicates that this is the last element of the list of free blocks (the first word with the freed pointer number contains the next pointer).
If you want to allocate and free more memory before playing this block again, the value will change when a new item is added to the end of the free list. This is one way to solve library decisions that affect what happens during undefined. In this case, the glibc developers took advantage of the fact that this behavior is undefined to make their memory allocator more economical.
If you run your program under valgrind, it will catch these errors for you. In any case, always stay away from undefined behavior, as it will probably differ on different platforms and even on different builds on the same platform (for example, debug vs release).
source share