Using memory allocation instead of asprintf