On 05/11/2012 03:00 PM, Cristian Rodríguez wrote:
El 11/05/12 15:06, David C. Rankin escribió:
Guys,
I need a bit of help. I've tried to follow the example after wchar_t * wcscat that explains why using mempcpy is better than using strcat or wcscat, but I run into a segmentation fault attempting to use the suggested mempcpy replacement. The code in libc.html (Ver: 2.15) is shown below. I had to add the declaration for 's' to get past an undefined error (should have told me something right then).
char * concat (const char *str, ...) { va_list ap; size_t allocated = 100; char *result = (char *) malloc (allocated);
if (result != NULL) { char *newp; char *wp; const char *s; ^^^^^^^^^^^^^^^^^^
Yes, there is this oversight also in the example code...
result = concat( myS1, myS2, myS3 );
^^ here here, missing sentinel ... should say.
concat( myS1, myS2, myS3, NULL );
Christian, Thank you! Moving the _GNU_SOURCE define solved the compiler warnings and it now compiles on gcc 4.6 and 4.7 without warning. However... I think the code hates me. Looking at concat, it is supposed to return the pointer to the null terminated concatenated string. I thought that was what it was doing with '*wp++ = '\0';' before calling realloc. --Nope, you are correct, it works fine as long as I concatenate a NULL by sending it as the last string. Thanks! Works either way on gcc 4.7: fprintf ( stderr, "The result is: %s\n", concat( myS1, myS2, myS3, NULL )); or char * result; result = concat( myS1, myS2, myS3, NULL ); fprintf ( stderr, "The result is: %s\n", result); 15:49 providence:~/dev/prg/ccpp/src-c/io/file> gcc -Wall -oct contest.c 15:49 providence:~/dev/prg/ccpp/src-c/io/file> ./ct myS1: This is the beginning myS2: of my string to test myS3: libc manual concat. The result is: This is the beginning of my string to test libc manual concat. Now to change it so it automatically add the NULL :) -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org