malloc() delivers uninitialized memory. Thus your NULL comparision won't work. You need to explicitely set the memory to zero, using memset.