0

My code compile but when I try to run it, I get a segmentation fault with this line

if (table[x] != NULL)

Here is the code of the load function

bool load(const char *dictionary)
{
    int i;
    for (i = 0; i < 10000; i++)
    {
        table[i] = NULL;
    }
    char new_word[46];
    FILE* file = fopen(dictionary, "r");
    char word[LENGHT + 1] = {0};
    while (fscanf(file, "%s", new_word) != EOF)
    {
        int x;
        x = 0;
        x = hash(new_word);
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            printf("Not enough memory");
            return false;
        }
        strcpy(n->word, new_word);
        n->next = NULL;
        if (table[x] != NULL)
        {
            node *temp;
            temp = table[x];
            table[x] = n;
            n->next = temp;
        }
        else
        {
            table[x] = n;
        }
        size_of_dictionary = size_of_dictionary + 1;
    }
    return true;
}

Here is the valgrind output

==7393== Memcheck, a memory error detector
==7393== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7393== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==7393== Command: ./speller texts/lalaland.txt
==7393== 
==7393== Invalid read of size 8
==7393==    at 0x40125E: load (dictionary.c:89)
==7393==    by 0x400964: main (speller.c:40)
==7393==  Address 0x699bd0 is not stack'd, malloc'd or (recently) free'd
==7393== 
==7393== 
==7393== Process terminating with default action of signal 11 (SIGSEGV)
==7393==  Access not within mapped region at address 0x699BD0
==7393==    at 0x40125E: load (dictionary.c:89)
==7393==    by 0x400964: main (speller.c:40)
==7393==  If you believe this happened as a result of a stack
==7393==  overflow in your program's main thread (unlikely but
==7393==  possible), you can try to increase the size of the
==7393==  main thread stack using the --main-stacksize= flag.
==7393==  The main thread stack size used in this run was 8388608.
==7393== 
==7393== HEAP SUMMARY:
==7393==     in use at exit: 608 bytes in 2 blocks
==7393==   total heap usage: 3 allocs, 1 frees, 4,704 bytes allocated
==7393== 
==7393== 56 bytes in 1 blocks are still reachable in loss record 1 of 2
==7393==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7393==    by 0x401202: load (dictionary.c:81)
==7393==    by 0x400964: main (speller.c:40)
==7393== 
==7393== 552 bytes in 1 blocks are still reachable in loss record 2 of 2
==7393==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7393==    by 0x5258E49: __fopen_internal (iofopen.c:65)
==7393==    by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==7393==    by 0x4011A3: load (dictionary.c:74)
==7393==    by 0x400964: main (speller.c:40)
==7393== 
==7393== LEAK SUMMARY:
==7393==    definitely lost: 0 bytes in 0 blocks
==7393==    indirectly lost: 0 bytes in 0 blocks
==7393==      possibly lost: 0 bytes in 0 blocks
==7393==    still reachable: 608 bytes in 2 blocks
==7393==         suppressed: 0 bytes in 0 blocks
==7393== 
==7393== For counts of detected and suppressed errors, rerun with: -v
==7393== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
/etc/profile.d/cli.sh: line 94:  7393 Segmentation fault      valgrind ./speller texts/lalaland.txt

here is the hash function. is setted hashmax to 10000.

unsigned int hash(const char *word)
{
    unsigned int hash = 5381;
    int c;

    while ((c = *word++))
    {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }
    return hash % HASHMAX;
}
1

Need to see the hash function to know for sure, but I'll bet that the hash value that is returned is out of range for the table array. Try printing the value of x just before the line that's generating the seg fault. Is it bigger than the number of elements in table[], i.e. 10000? Or is it negative?

Kudos for identifying the specific line with the seg fault. There are so many people that don't get that far and this is half the battle!

1
  • I added the hash function. I used DJB2 – Thomaslusignan Jul 2 '20 at 21:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .