My program gets “segmentation fault” in “strcat(string,stringaux)” and it is strange because I am sure that string always has space for adding the new stringaux due to before call strcat I check if size of the destiny plus new stringaux is less than string capacity:
char string[100];
char stringaux[3];
void Function()
{
if ((strlen(string)+strlen(stringaux))<100)
{
strcat(string,stringaux)
}
}
In this way strcat() never can make string be longer than 100. But still I get SEGMENTATION DEFAULT.
I have been reading, and the explanation can be that the Stack gets full due to I have many local variables or …
Is there anybody can tell me some ideas about why SEGMENTATION DEFAULT is produced in strcat() when I am sure that is not because there is not space in the destiny string?
strlen returns the length of the string without the null byte at the end. That means the actual storage requirement for your string is (strlen(string) + 1) and (strlen(stringaux) + 1).
No that’s even worse. You are reusing the dest in the same call. The result is indeterminate. If for some reason the implementation of snprintf decides internally to say put a NUL in the first byte before any copying is done, you have truncated one of the input strings.
If you were to use a different destination, that would be ok.