fixed segmentation fault generated from mem_alloc