-2

I am relatively new to programming, and this is my first term working in C. So it is entirely possible that this could be a really simple mistake, or it is also possible that the explanation of what I did wrong could go over my head.

My program reads in a text file a builds a concordance in a hashmap which tells the user which words are in the text and how many times they occur. However, I continue to segfault (code 11), whenever I use the myCompare function. Here is its code:

int myCompare(void *s1, void *s2)
{
    printf("\n\nStarting myCompare...");
    printf("\n*s1: %s", (char *)s1);
    printf("\n*s2: %s", (char *)s2);

    char *key1 = (char *)s1;
    printf("\n\nkey1: %s", key1);
    char *key2 = (char *)s2;
    printf("\nkey2: %s", key2);

    return (strcmp(key1, key2));
}

I added some print statements to it and the function that calls it to give me a better idea of what is passing and exactly where it is segfaulting, but I am not sure why it is happening. Here is the output, if anyone needs more of the code to understand my mistake, just let me know. Thanks guys

struct hashLink {
    void* key;
    void* value;
    struct hashLink * next;
};

struct hashMap {
    hashLink ** table;
    int tableSize;
    int count;
};

...

void removeKey (struct hashMap * ht, void* k, comparator keyCompare, hashFuncPtr hashFunc)
{

struct hashLink *previousLink;
struct hashLink *currentLink;
int index = 0;

printf("\n\nVariables Declared");
printf("\nPre-hash index: %d", index);
index = ((*hashFunc)(k) % ht->tableSize);
void* keyPtr = &ht->table[index]->key;

printf("\nPost-hash index: %d", index);
printf("\nkeyPtr value: %p", keyPtr);
printf("\nk value: %p", k);

int testCompare = (*keyCompare)(k, keyPtr);
printf("\nkeyCompare: %d", testCompare);

if ((*keyCompare)(k, keyPtr) ==)){

... } 

Terminal Output:

Starting hash2...
Passed key: and
value of r: 617

Post-hash index: 7
keyPtr value: 0x10e65dcd8
k value: 0x10e65df24

Starting myCompare...
*s1: and
*s2:

key1: and
key2:  
keyCompare: 65

Starting myCompare...
*s1:  
*s2: and

key1:  
Segmentation fault: 11
logout
GullySquad02
  • 43
  • 1
  • 6
  • 2
    Please explain how you are calling `myCompare`. – Rufflewind Mar 11 '15 at 10:09
  • Add \n after all %s and rerun your program to see if that changes the printing. I don't think the problem is related to the function myCompare. – Support Ukraine Mar 11 '15 at 10:12
  • Are you sure neither of `s1` and `s2` is `NULL`? – rubikonx9 Mar 11 '15 at 10:17
  • What does valgrind says? – Olle Härstedt Mar 11 '15 at 10:19
  • 1
    I guess `ht->table[index]->key` is a `char *` . Use `void* keyPtr = ht->table[index]->key;` instead – Michele d'Amico Mar 11 '15 at 10:19
  • @g.tsh there is a possibility that it could pass a NULL value, although I have done a check before passing values, and if NULL I set them to something... It still gave a segfault – GullySquad02 Mar 11 '15 at 10:21
  • 1
    Better to check for `NULL` at the point of use, not to rely on other code. – Weather Vane Mar 11 '15 at 10:22
  • @Micheled'Amico - I tried that, but it segfaults at the `ht->table[index]->key` line – GullySquad02 Mar 11 '15 at 10:24
  • 1
    perhaps `ht->table[index]->key` is `NULL` or was not properly allocated – M.M Mar 11 '15 at 10:36
  • 2
    To get good help post a [MCVE](http://stackoverflow.com/help/mcve). As things currently stand the problem is in code you haven't posted. – M.M Mar 11 '15 at 10:37
  • The problem is not a NULL pointer. Notice that the printing went fine inside myCompare when using s1 and s2 but failed using key1 and key2 even though they are just simple copies of s1 and s2. Further the problem is not in myCompare. Someone from the outside must be overwriting the memory used by myCompare. Could it be an ISR or another thread? – Support Ukraine Mar 11 '15 at 10:40
  • `ht->table[index]` can be null, so dereferencing `->key` is not allowed: `keyPtr = ht->table[index]->key` – wildplasser Mar 11 '15 at 11:32
  • @wildplasser - if you look at the output, you can see that the seg fault happens a long time after dereferencing key. So that can't be the problem. – Support Ukraine Mar 11 '15 at 11:44
  • Well: why don't you `printf("ThePoinTer: %p\n", `ht->table[index])` , before dereferencing it? – wildplasser Mar 11 '15 at 12:01
  • BTW you probably dont want the `&` in `void* keyPtr = &ht->table[index]->key;` , since it allready is a pointer. (but you did not show us the struct definition. – wildplasser Mar 11 '15 at 12:08
  • @GullySquad02 - The supplied code doesn't match the terminal output. The code only has one call of myCompare while the terminal output has two. In other words - we don't have the failing part of the code so we can't help. But for sure it isn't your the myCompare function causing problems. – Support Ukraine Mar 11 '15 at 12:42
  • The issue seems to be coming from how I am trying to de-reference `ht->table[index]->key`. – GullySquad02 Mar 11 '15 at 13:15

1 Answers1

1

If you code segfaults, then you probably are trying to operate with the pointers that contain garbage and do not actually point to any data that you own. Try using valgrind.

  • Compile your code without optimization and with debugging symbols included

     g++ -O0 -g ./your_program.cpp -o ./your_program
    
  • Use valgrind's memory checker

     valgrind --leak-check=yes --track-origins=yes ./your_program
    

It is likely that valgrind will tell you what exactly is wrong with your code.

victor
  • 141
  • 2