Check for NULL pointers after calling malloc