1

I couldn't find anything similar to this anywhere. I have an array of pointers to objects (a linked list) for a hash table:

LinkList * table[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
    table[i] = NULL;

In one of my functions for my hash table class, there I need to call the one of the functions on the LinkList object in the table array. I'm calling it as such:

void HashMap::add_customer(string first, string last, string phone) {   
    int hash = get_hash(phone);
    if (table[hash] == NULL) {
        table[hash] = new LinkList;
    }
    table[hash]->add_customer(first, last, phone); // I HATE THIS LINE
}

Everything compiles fine, but when I execute the table[hash]->add_customer() line in runtime, I get a Segmentation Fault error. When this line is commented out, I get no errors, but obviously, I can't add any customers to my hash table. Is this not the right syntax?

7
  • 1
    Are you ensuring that each value in the array is initialized to NULL and also set to NULL when an item is removed? Commented Jun 12, 2012 at 23:38
  • Do some adds work, and others don't? What happened before the fault? Commented Jun 12, 2012 at 23:40
  • They are all initialized to NULL and no removes have happened before this fault happens. This happens everytime HashMap::add_customer is called. Commented Jun 12, 2012 at 23:47
  • "This happens everytime HashMap::add_customer is called." - That is a different statement than "when I execute the table[hash]->add_customer() line in runtime, I get a Segmentation Fault error". Which is it? Is it that line that always causes the problem? If so then it is the dereference causing the fault. Commented Jun 12, 2012 at 23:48
  • Also, per the declaration of table that you have shown, it is not initialized to all NULL's, so do you do it later? Why not use LinkList *table[TABLE_SIZE] = {}? Commented Jun 12, 2012 at 23:50

3 Answers 3

2

You have to initialized your array of pointers to NULL, since they'll be allocated on the stack/heap or wherever with junk values ....

LinkList * table[TABLE_SIZE];
memset(table, NULL, sizeof(LinkList *) * TABLE_SIZE);

assuming you've initialized correctly then please check your hashes and assert that indeed they are hash < TABLE_SIZE

try this:

 int hash = get_hash(phone) % TABLE_SIZE;
Sign up to request clarification or add additional context in comments.

3 Comments

Nice catch. Missing modulus operator perhaps?
Need to modulo on the initialization of the table element too.
yeah I shouldn't have being focusing just on that line, I guess highlighting threw me off ... thank you. but I did like // I HATE THIS LINE
0

This snippet looks right, for the hash map's behavior. I'd check the add_customer method in your LinkList implementation. Try using your linked lists outside of this hash map, just to make sure they work as intended.

5 Comments

It's not entirely clear, but if the seg fault is occurring on that line then it never even got to the add_customer function, dereferencing the pointer is what caused it.
That's right. I have separate implementation that just uses the LinkList and add_customer works fine there. Its just the pointer dereferencing that's giving me issues.
Yes, but we all we know now is that commenting the line "fixes" it. @Joshua, try using a debugger to follow where the segfault is happening. If you're in an IDE, there are probably nice tools for this already. If you're at a terminal (assuming Unix here), add the -ggdb flag to your compile, then run gdb [executable name here, a.out if you didn't change anything]. It should nicely identify the error location.
Ok, gotcha, have you run a debugger? (by the way, my explanation there wasn't meant to be condescending, it just sounded like you might possibly be newer to this particular area)
Also, make sure your pointers are actually null. If your code is unmanaged, that array is just using whatever values were already in that area of member.
0

Try this:

LinkList * table[TABLE_SIZE] = {};

That nulls out your pointers to begin with.


Can you confirm get_hash returns values < TABLE_SIZE? Add an assert or something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.