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

  • SEARCH
  • Home
  • 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 8683845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:08:58+00:00 2026-06-12T22:08:58+00:00

Basically I am trying to build a hash structure of cstrings that I can

  • 0

Basically I am trying to build a hash structure of cstrings that I can re-assign the member stringnode_ref *nodes to a larger dimension array (malloc a new one, re-hash the existing values, and delete the old one) when the hash begins to fill up.

stringtable.c:

#include <stdio.h>
#include "stringtable.h"

struct stringnode{
    hashcode_t key;
    cstring value;
};

struct stringtable{
    size_t dim;
    size_t numEntries;
    stringnode_ref *nodes;
};

stringtable_ref new_stringtable(){
    size_t index = 0;
    stringtable_ref sTable = malloc(sizeof(struct stringtable));
    sTable->dim = 31;
    sTable->numEntries;
    sTable->nodes = malloc(31 * sizeof(struct stringnode));
    for( index = 0; index < 31; index++ ){
        sTable->nodes[index]->key = 0;
        sTable->nodes[index]->value = NULL;
    }
    return sTable;
}

stringtable.h:

#ifndef __STRINGTABLE_H__
#define __STRINGTABLE_H__

#include <stdlib.h>
#include <inttypes.h>

typedef char *cstring;

typedef uint32_t hashcode_t;

typedef stringtable *stringtable_ref;

typedef stringnode *stringnode_ref;

stringtable_ref new_stringtable();

#endif // __STRINGTABLE_H__

oc.c:

#include <stdlib.h>
#include <stdio.h>
#include "stringtable.h"

int main( int argc, char **argv ){
    stringtable_ref table = new_stringtable();
    return EXIT_SUCCESS;
}

compiling with:

gcc -g -O0 -Wall -Wextra -std=gnu99 -c stringtable.c
gcc -g -O0 -Wall -Wextra -std=gnu99 -c oc.c
gcc -g -O0 -Wall -Wextra -std=gnu99 -o oc stringtable.o oc.o

this all compiles fine with no errors but upon declaring stringtable_ref table = new_stringtable(); in the main(), the program segfaults at sTable->nodes[index]->key = 0;. Any thoughts?

  • 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-12T22:08:59+00:00Added an answer on June 12, 2026 at 10:08 pm
    sTable->nodes = malloc(31 * sizeof(struct stringnode));
    for( index = 0; index < 31; index++ ){
        sTable->nodes[index]->key = 0;
    

    here’s the problem: the segfault occurs because you don’t allocate memory for sTable-nodes[index], so it may be garbage (when I examined your code on GDB under OS X, it was actually NULL).

    Solution: add the following line as the first line of the body of the for loop:

    sTable->nodes[index] = malloc(sizeof(*(sTable->nodes[index])));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build some image algebra code that can work with images (basically
I'm trying to build a TreeView in WPF that has categories. Basically the structure
I'm trying to build a hash from an array. Basically I want to take
I'm trying to build a multidimensional array dynamically. What I want is basically this
Basically i am trying to build an application that uses SSIS to run a
I am trying to build a rails app that's basically just a text-editor (with
Basically i am trying to build a mono video player that uses VLC and
I'm basically trying to build an html ul/li nested list from a multidimensional array
I have a listbox that I am trying to build a triggeraction for. Basically
I am trying to to build a Felix bundle in Eclipse. This basically includes

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.