Avoiding segmentation fault with malloc