Thread: segment fault related to strlen.S

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    16

    segment fault related to strlen.S

    I was wondering why the marked line in this function always gave "segmentation fault" error at the end of run. No warning at compiling, though.
    Code:
    void get_hashes(unsigned int hash[],  const char *in)
    {
        int pos = strlen(in);            //causing Segmentation fault at end of run!
        hash[0] = RSHash(in, pos);
        hash[1] = DJBHash(in, pos);
        hash[2] = FNVHash(in, pos);
        hash[3] = JSHash(in, pos);
        hash[4] = PJWHash(in, pos);
        hash[5] = SDBMHash(in, pos);
        hash[6] = DEKHash(in, pos);
        hash[7] = murmur(in, (uint64_t) pos, (uint64_t) pos);
    }
    And when I tried GDB to debug, the error message is:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    strlen () at ../sysdeps/x86_64/strlen.S:106
    106    ../sysdeps/x86_64/strlen.S: No such file or directory.
    Can anybody help me with it?
    Thanks a lot!
    Attached Files Attached Files
    Last edited by Yifangt; 03-07-2016 at 10:58 AM. Reason: Attach the whole program

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post a complete (preferably simplified) program that exhibits the problem. You should also be sure you are compiling with maximum warnings.

    What are you passing to the function for "in"?

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    16
    Thanks Matt!
    What I am passing to the function for "in" is a string.
    A test code was attached.
    Last edited by Yifangt; 03-07-2016 at 11:16 AM. Reason: Tidy up.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Note that while it appears you're declaring an array with 22 strings, you have a mistake such that there are only 21. Therefore, when you pass the last element of the array to the "get_hashes()" function, you are accessing the array out of bounds.

    The mistake is, when initializing the array, you left out a comma at the end of one of the rows. In C, adjacent string literals are concatenated during compilation. So by not having that comma, what appears to be two separate strings is actually one string.

    Note that this behavior of automatic string concatenation can be used to your advantage, so you do not have to use excessively long lines.

    For instance, as an example with a single string, this:

    Code:
    const char *str = "CCAACAGAATACCGATGATTCCCGTCTTCACCTGCCTGTTTTGCCTTTTCCTGGCATCGTTTCTTGCGGAAATGGCGCAGACCGACATGGGATATTTTATTCCTTGAGTTCTGGTC"
    ... is equivalent to this:

    Code:
    const char *str = "CCAACAGAATACCGATGATTCCCGTCTTC"
                      "ACCTGCCTGTTTTGCCTTTTCCTGGCATC"
                      "GTTTCTTGCGGAAATGGCGCAGACCGACA"
                      "TGGGATATTTTATTCCTTGAGTTCTGGTC";
    Note there are no commas between the strings, to ensure they are concatenated by the compiler.
    Last edited by Matticus; 03-07-2016 at 11:39 AM.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    16
    Note there are no commas between the strings, to ensure they are concatenated by the compiler.
    Thank you so much!
    This bugged me several weeks, and I had thought the problem is the unsigned char that I changed to const char, which I do not have full understanding.
    Thank you again!

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "const char" is good, since string literals are not allowed to be modified.

    You might want to consider calculating the number of elements in the array to ensure subsequent code stays within the bounds of the array.

    Example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const char *str[] = { "ABC", "BCD", "CDE", "DEF", "EFG",
                              "FGH", "GHI", "HIJ", "IJK", "JKL" };
    
        printf("Number of elements in the array: %d\n", sizeof(str)/sizeof(char*));
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segment-fault happens when use pthread_mutex_trylock
    By pooyair in forum C Programming
    Replies: 4
    Last Post: 09-13-2012, 12:02 PM
  2. Segment fault in the program
    By sanddune008 in forum C Programming
    Replies: 6
    Last Post: 04-27-2012, 11:11 AM
  3. Segment Fault with shared memory set using mmap
    By AKalair in forum C Programming
    Replies: 12
    Last Post: 11-18-2011, 02:20 PM
  4. segment fault with the program
    By sanddune008 in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2011, 10:23 AM
  5. segment fault?
    By major_small in forum C++ Programming
    Replies: 4
    Last Post: 09-18-2003, 07:18 PM

Tags for this Thread