line 7 be breaking stuff. it leaks the memory you just got, and worse: you can't realloc a const char* (line 1).
unless you need your strings to be exactly sized to content (say, you have very little system memory on your wristwatch?) you should use char arrays of a fixed size (say 200 or whatever characters, consider a typedef).
now, check the actual # of bytes from line 5.
I think sizeof isnt doing what you think its doing.
you want strlen(val)+ something, and do not forget to leave space for the terminal zero.
Not specified in Standard C or POSIX, but available on Linux (glibc) as well as *BSD:
The functions asprintf() and vasprintf() are analogs of sprintf() and vsprintf(), except that they allocate a string large enough to hold the output including the terminating null byte, and return a pointer to it via the first argument.
1 2
int asprintf(char **strp, constchar *fmt, ...);
int vasprintf(char **strp, constchar *fmt, va_list ap);
I think I'll stick with C/C++ functions that cppreference has documentation on, thankyouverymuch. :D
Personally I'll work with C++ std::string first and foremost. And when I need some C string mangling I'll make do with the ol' stand-by stuff of strcpy, etc. :)