[Solved] strcat for dynamic char pointers


From section 7.20.3.2 The free function of the C99 standard:

The free function causes the space pointed to by ptr to be deallocated, that is, made
available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if
the argument does not match a pointer earlier returned by the calloc, malloc, or
realloc function, or if the space has been deallocated by a call to free or realloc,
the behavior is undefined.

And str1 (aka first) was not allocated by one of the three dynamic memory allocating functions, causing the crash. Same for second also.

if don’t free then the memory allocated for the first will be leaked(if am understood properly).

Memory will be leaked if malloc(), realloc() or calloc() is used and the memory is not free()d at a later point. As the string literal "Testone" is not dynamically allocated it must not be freed.

Note that the pointer NULL check prior to invoking free() is unrequired as free(), as described above, does nothing if the pointer argument is NULL.


Do I cast the result of malloc?

solved strcat for dynamic char pointers