[ad_1]
I am using freeBSD arm64 on my pi4 to test some C code. I am finding strange behavior.
I know that i should be doing the parsing a bit differently but I like to get the basics working first.
char* combineString(int num, ...) {
char* finalStr;
finalStr = calloc(600, sizeof(char));
va_list vaList;
/* initialize */
va_start(vaList, num);
printf("%i\n", num);
for (int x = 0; x < num; num++) {
char* str = va_arg(vaList, char*);
strcat(finalStr, str);
printf("%s\n", finalStr);
}
va_end(vaList);
return finalStr;
}
Somehow, this program loops 5 times instead of 2 (the number of arguments I told the function I had).
The number of arguments is indicated by ‘num’.
combineString(2, "f", char* type here)
will produce:
f
fchartypehere
fchartypehere
fchartypehere
fchartypeherechartypehere
where the double chartypehere indicates it wrote that char* twice even though it should only be looping twice? I am using gcc to compile this and whenever i use gdb to get some relevant information i get this:
Program received signal SIGSEGV, Segmentation fault.
Address not mapped to object.
strcat (s=<optimized out>, append=<optimized out>) at /usr/src/lib/libc/string/strcat.c:46
46 /usr/src/lib/libc/string/strcat.c: No such file or directory.
I suppose its just coincedentally running 5 times due to undefined behavior but i am not sure where in my code exactly its causing that?
[ad_2]