0

quick question- I am getting a runtime error that looks like this:

dictionary.c:93:14: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/string.h:130:14: note: nonnull attribute specified here Segmentation fault

what does this mean? Relevant code down below. Thanks!

bool load(const char *dictionary)
{

    FILE *file = fopen(dictionary, "r"); // opening up and reading dictionary word by word
    char word[LENGTH + 1];
    if (file == NULL)
    {
        return false;
    }
    while (fscanf(file, "%s", word) != EOF) // look for string, put that string in variable called word
    {

        //malloc a new node for the word
        node *new_node = malloc(sizeof(node));

        if (new_node == NULL)
        {
            unload();
            return false;
        }
      strcpy(new_node -> word, word); // copy word into node

      int index = hash(word); // hash word

      //check for an empty linked list with this hashed word value

        if (hashtable[index] == NULL)
        {
             hashtable[index] = new_node;
            new_node -> next = NULL;
        }
        else // insert into an already made linked list
        {
             new_node -> next = hashtable[index];
            hashtable[index] = new_node;
        }

        wordCounter++;
    }
    fclose(file);

    return true;
}



// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if (wordCounter > 0)
    {
        return wordCounter;
    }
    else
    {
        return 0;
    }
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    node *cursor = NULL;                // initializing cursor to head

    for (int f = 0; f < HASHTABLE_SIZE; f++)    // iterating through the hashtable to free up nodes
    {
        if (hashtable[f] != NULL)
        {
            f++;
        }
        else
        {
            cursor = hashtable[f];              // cursor to first node

                while (cursor != NULL)
            {
                node *temp = cursor;            // cursor follows along each node till all are freed
                cursor = cursor -> next;
                free(temp);
            }
        }

        return true;
    }
    return false;
}
  • Which line is 93? – Allan Clayton Jul 26 '18 at 19:16
  • strcpy(new_node -> word, word); // copy word into node – Wills Mckenna Jul 26 '18 at 19:29
0

I'm going to guess that your struct def looks like this:

typedef struct node
{
    char *word;
    struct node* next;
}
node;

Since word is a pointer, it has to have memory allocated to it using malloc or one of its cousins. When a node is malloc'd, that process allocates the pointer word, but not the memory that the pointer would point to.

The result is that the strcpy is being passed an invalid first parameter.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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