0

My size function is not working and I don't understand why

unsigned int size(void)
{
    int c = 0;
    for (int i = 0; i < N; i++)
    {
        node *wNode = table[i];
        while (wNode->next != NULL)
        {
            c++;
            wNode = wNode->next;
        }
    }
    return c;
}

I have looked over at similar size function which is quite similar to my design, but it still is not working.

Don't hesitate to ask me for other functions, I can provide those too! But the errors only come from this function.

EDIT: Thanks to the kind person for helping me figure this out

if (wNode != NULL)
{
    c++;
    while (wNode->next != NULL)
    {
        c++;
        wNode = wNode->next;
    }
}

1 Answer 1

1

This program will give a memory fault if there is a list index with no entries, as with the small dictionary. If table[i] is NULL, wNode->next does not exist and this line while (wNode->next != NULL) will give an error.

1
  • I thought it was something like that! I had set up an if statement to check for that and to increment the counter, but I had set it up wrong so I just thought: "Okay well I don't need that then"
    – Setaganga
    Commented Jul 17, 2022 at 17:00

You must log in to answer this question.

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