Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:06:01+00:00 2026-06-01T21:06:01+00:00

I am trying to write a simple hash table in C and I am

  • 0

I am trying to write a simple hash table in C and I am getting a very strange segfault when testing it with the code shown my main().

The Design: I have a hash table with an underlying array of size 10,000. I hold a double pointer to the start of an array of struct node_t (or node) pointers. When I want to put() something into the hash table, I check if the node element at the appropriate location is NULL. If it is, I create a new node to fill the spot, otherwise, if there are collisions, I build a linked list off of the colliding node.

The Scenario: In main(), I am trying to put() the number 3328 into the hash table. Instead, the program segfaults. This makes no sense to me as the previous put() works fine and you can clearly see that I set all the initial pointers to NULL. As far as I know, the pointer that refers to hash table location 3328 is not getting set to NULL because when I dereference it in the put() function, that’s when it segfaults. My main function looks like it should set all the pointers to NULL just fine though…

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int TABLE_SIZE = 10000;

typedef struct node_t {
    int key;
    int value;
    struct node_t* next;
} node;

node** table;

inline node** get_node_address(int key) {
    key = (key > -key ? key : -key) % TABLE_SIZE;
    return (node**) (table + key * sizeof(node*)); 
}

inline node* new_node(int key, int value) {
    node* n = malloc(sizeof(node));
    n->key = key;
    n->value = value;
    n->next = NULL;
    return n;
}

void put(int key, int value) {
    node** n = (node**) get_node_address(key);
    node* iterator = (node*) *n;

    if (*n == NULL) {
        *n = new_node(key, value);
    } else {
        while (iterator->next != NULL)
            iterator = iterator->next;

        iterator->next = new_node(key, value);
    }
}

int* get(int key) {
    node* iterator = (node*) *get_node_address(key);

    while (iterator != NULL && iterator->key != key) {
        iterator = iterator->next;
    }

    if (iterator == NULL)
        return NULL;
    else
        return &(iterator->value); 
}

int main() {
    printf("Starting..\n");

    int i;
    table = malloc(sizeof(node*) * TABLE_SIZE);
    memset(table, 0, sizeof(node*) * TABLE_SIZE);

    for (i = 0; i < TABLE_SIZE; i++) {
        table[i] = NULL;
        printf("setting %x\n", &table[i]);
    }

    printf("before bad: %x\n", *get_node_address(3327));
    printf("bad address: %x\n", *get_node_address(3328));
    printf("last address: %x\n", table + sizeof(node*) * TABLE_SIZE);

    printf("Hashing...\n");

    put(3328, 3338);
    printf("%d", *get(3328));

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:06:02+00:00Added an answer on June 1, 2026 at 9:06 pm

    There’s at least one problem:

    inline node** get_node_address(int key) {
          key = (key > -key ? key : -key) % TABLE_SIZE;
          return (node**) (table + key * sizeof(node*)); /* <---- */
    }
    

    You mustn’t multiply key. Because of the way pointer arithmetic works in C, table + key yields the key-th element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write simple Visual Studio Add-In for code generation. In my
I trying to write a simple function to call unmanaged code from managed code.
I am trying to write a simple tool using Shoes. This will indent code
i'm trying to write simple tcp\ip client-server. here is server code: internal class Program
I'm trying to write simple proxy server for some purpose. In it I use
I'm trying to write a simple maze search program in prolog, before I add
I'm trying to write a simple javascript function that will clone some html input
I'm trying to write really simple iOS5 app just searching for specific type of
I'm trying to write a simple game/utility to calculate poker odds. I know there's
I'm trying to write a simple program in VC++ which will just initialize the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.