Fixing realloc problems:
1) if size is 0, then behaviour is undefined
2) in C99 if realloc returns NULL, and size is not zero (errno == ENOMEM) input buffer of memory is needed to be freed manually.

Whitepaper of POSIX standart:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html