Use xrealloc instead of if(ptr) ptr = xmalloc().