Segmentation fault Error

This is a discussion on Segmentation fault Error within the C Programming forums, part of the General Programming Boards category; hey, i'm trying to compare 2 strings but it keep on hanging an my if statement. and when i run ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21