Results 1 to 9 of 9
Hello.
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 ...
- 03-24-2009 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 4
segmentation fault in strcat()
Hello.
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?
Thanks in advance.
- 03-24-2009 #2
Hi sankari27,
I tried your code snippet and it worked fine, could you show us the rest of your program....Gerard4143Make mine Arch Linux
- 03-24-2009 #3
Note sankari27,
you really shouldn't use string and stringaux as variable names especially when your using the string library...Gerard4143Make mine Arch Linux
- 03-25-2009 #4Just Joined!
- Join Date
- Oct 2008
- Posts
- 4
Hi gerard4143!
The piece of code that I wrote at the beginning is just an example. That example is working fine also to me and sorry for the name of the variables they are also example.
For me it is very hard to write the original code, the function is called every certain time due to the use of interruption.
What I wanted to show in the example is more or less what is happening in my original code. There is a segmentation fault in strcat() and I am sure that the destiny string has enough space. So WHY the Segmentation appears?
Thank you.
- 03-25-2009 #5
Are you sure seg.fault happens only because of strcat()...have you tried using gdb ..or...Nemiver which has GUI and easy to use.
else use simple strace command- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 03-25-2009 #6
- 03-25-2009 #7Just Joined!
- Join Date
- Mar 2009
- Posts
- 2
Hi sankari27,
Its difficult to tell from the snippet you have pasted. It depends on the scope of variables string and stringaux and on the values assigned to them.
strcat() requires that the destination be initialized. If string is not initialized, then we have 2 conditions.
1. If string is declared global, the it would be null. strcat exits as soon as it sees a null. IN this case there should not be an error.
2. If string is a local variable, then strcat() dumps core as it tries to access uninitialized memory segment.
If string is initialized and stringaux is null terminated then we may need the rest of the code to help you out.
- 03-29-2009 #8
compile your code using
now run it with gdb$ gcc filename.c -o out -O0
Now use the run command$ gdb out
(gdb) run
It should tell where the problem lies. Post it here...
- 03-30-2009 #9Just Joined!
- Join Date
- Oct 2008
- Posts
- 4
I solved it
Thanks everybody!
Finally I corrected the error!


Reply With Quote
