Thread: Segmentation fault Error

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Segmentation fault Error

    hey, i'm trying to compare 2 strings but it keep on hanging an my if statement. and when i run through bash i get Segmentation fault Error. the code:

    my struct
    Code:
    struct Entry{
      char * string;
      int count;
      
    };
    Entry hash_table[HASH_TABLE_SIZE];
    It hangs at this statement. I'm basically checking if there is an entry in the table already. if there is then i'll increment the count and send it's position to another bit of code.
    Code:
    	if(strcmp(hash_table[j].string, tag) == 0){
    	         hash_table[j].count++;
    	         return j;
    	}
    any1 spot something wrong?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, at least not in the portion of code that you showed. One possibility is that string is an uninitialised pointer. Another possibility is that you did not null terminate the strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I need to use

    struct Entry ... instead of just Entry, unless you have a typedef for it, when I declare hash_table

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    i have:
    Code:
    typedef struct Entry Entry, *EntryPtr;
    as my type def.

    my function is fully operational without the if statement, it's just when I add it I get an error.
    @laserlight, when i declare it in my struct, isn't the null character already assigned to it? because i also have this code:
    Code:
    if(!(hash_table[j].string)){}
    which checks if the position is empty and enters in the data if it is. so i assumed that if there is nothing but a null character in the string then
    Code:
    if(strcmp(hash_table[j].string, tag) == 0){}
    is false if the tag ="dog" or something.

    Is there any other method to see if the 2 strings are equal?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by unknown_
    when i declare it in my struct, isn't the null character already assigned to it?
    No. It might be uninitialised or a null pointer, or perhaps it actually points to something that may or may not be terminated by a null character, but with what you posted, it is hard to say.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    this is my code. i'll just delete it after.
    Code:
    GONE
    Last edited by unknown_; 03-21-2010 at 01:33 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    calloc(sizeof(tag), sizeof(char));

    you allocating 4 bytes which is not enough to store your string

    you need

    calloc(strlen(tag)+1, sizeof(char));

    Also

    if(!(hash_table[j].string)) - this check should be done before you call strcmp, otherwise you could pass the null pointer to strcmp
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Quote Originally Posted by vart View Post

    if(!(hash_table[j].string)) - this check should be done before you call strcmp, otherwise you could pass the null pointer to strcmp
    that seemed to do the trick. Thanks every one!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM