The character array returned by the strdup(f) function does not have enough space to include itself.
char *g = strcat(strdup(f),strdup(f));
The array used as the first argument of the function must have enough space so that you can add a string to it. For a reduced call, its size must be at least 2 * strlen( f ) + 1 .
In addition, there is a memory leak, because the address of the dynamically created array for the second argument is lost, and the created array cannot be deleted.
You cannot create new lines on the fly.
If this is a C ++ program, then the standard class std::string should be used. Then you can just write
std::string s( f ); s += f;
Or as a temporary expression
std::string( f ) + f
If this is a C program, the sample code might look like this.
char *s = malloc( 2 * strlen( f ) + 1 ); strcat( strcpy( s, f ), f ); //... free( s );
The same thing in C ++ will look like
char *s = new char[2 * strlen( f ) + 1]; strcat( strcpy( s, f ), f ); //... delete [] s;
strdupfunctionstrdupgeneric and not even standard. - 伪位蔚蠂慰位蠀蟿