Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Hi sankari27,

    I tried your code snippet and it worked fine, could you show us the rest of your program....Gerard4143
    Make mine Arch Linux

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Note sankari27,

    you really shouldn't use string and stringaux as variable names especially when your using the string library...Gerard4143
    Make mine Arch Linux

  4. #4
    Just 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.

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    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
    -------------------

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by sankari27 View Post
    For me it is very hard to write the original code, the function is called every certain time due to the use of interruption.
    Do you mean signal or interrupt...is this kernel code?
    Make mine Arch Linux

  7. #7
    Just 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.

  8. #8
    Just Joined! ManishSinha's Avatar
    Join Date
    Dec 2007
    Location
    Manipal,India
    Posts
    28
    compile your code using
    $ gcc filename.c -o out -O0
    now run it with gdb

    $ gdb out
    Now use the run command
    (gdb) run

    It should tell where the problem lies. Post it here...

  9. #9
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4

    I solved it

    Thanks everybody!

    Finally I corrected the error!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...