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 6059327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:41:53+00:00 2026-05-23T08:41:53+00:00

I have a structure of type: struct hashnode_s { struct hashnode_s *next; char *key;

  • 0

I have a structure of type:

struct hashnode_s {
    struct hashnode_s *next;
    char *key;
    ValueType tag;
    union
    {
        int IntegerValue;
        char *StringValue;
    } u;
    int IsInCycle;
};

And when I add an item of a type String, I have can, the code for it is

int hashtbl_InsertString(HASHTBL *hashtbl, const char *key, const char *value)
{
    struct hashnode_s *node;
    hash_size hash;

    hash = SearchForHashIndex(hashtbl, key,value);

    if(hash == -1)
    {
        hash=hashtbl->hashfunc(key);
    }
    /* adding the first node  if not applicable (this is based on value string)*/

    if(hashtbl->nodes[hash]== NULL)
    {
        node = malloc(sizeof(struct hashnode_s));
        node->key = key;
        node->tag = StringConst;
        node->u.StringValue = value;
        node->next = NULL;
        hashtbl->nodes[hash] = node;
    }
    else
    {
        node = hashtbl->nodes[hash];

        if(node->next ==NULL)
        {
            struct hashnode_s *nextNode;
            nextNode = malloc(sizeof(struct hashnode_s));

            if(strcmp(node->u.StringValue,key)==0)
            {
                /* set next */
                nextNode->key = key;
                nextNode->tag = StringConst;
                nextNode->u.StringValue = value;
                nextNode->next = NULL;
                node->next = nextNode;
                hashtbl->nodes[hash] = node;
            }
            else if(strcmp(node->key, value)==0)
            {
                /* switch node positions if the key */
                nextNode->key = key;
                nextNode->tag = StringConst;
                nextNode->u.StringValue = value;
                node->next = NULL;
                nextNode->next = node;
                node = nextNode;
                hashtbl->nodes[hash] = nextNode;
            }
        }
        else
        {
            while(node)
            {
                struct hashnode_s *nextNode;
                nextNode = malloc(sizeof(struct hashnode_s));

                /* TESTING PURPOSES ONLY
                printf("#define %s %s\n",node->key,node->u.StringValue);
                printf("%s==%s\n",node->u.StringValue,key);
                printf("%s==%s\n\n\n",node->key, value);
                */

                if(strcmp(node->u.StringValue,key)==0)
                {
                    nextNode->key = key;
                    nextNode->tag = StringConst;
                    nextNode->u.StringValue = value;
                    nextNode->next = NULL;
                    node->next = nextNode;
                    return 0;
                }
                else if(strcmp(node->key, value)==0)
                {
                    nextNode->key = key;
                    nextNode->tag = StringConst;
                    nextNode->u.StringValue = value;
                    node->next = NULL;
                    nextNode->next = node;
                    node = nextNode;
                    return 0;
                }
                node = node->next;
            }
        }
    }
}

But when I add an item of the type integer. It throws a segmentation fault for some reason?

Here’s that code.

int hashtbl_InsertValue(HASHTBL *hashtbl, const char *key, int integerValue)
{
    struct hashnode_s *node;
    hash_size hash;

    hash = SearchByKey(hashtbl, key);
    if(hash == -1)
    {
        hash=hashtbl->hashfunc(key);
    }

    if(hashtbl->nodes[hash] ==NULL)
    {
        node = malloc(sizeof(struct hashnode_s));
        node->key = key;
        node->tag = IntegerConst;
        node->u.IntegerValue = integerValue;
        node->next = NULL;
        hashtbl->nodes[hash] = node;
        return 0;
    }
    else
    {
        node = hashtbl->nodes[hash];
        //Check(hashtbl);
        while(node)
        {
            if(strcmp(node->u.StringValue,key)==0)
            {
                struct hashnode_s *nextNode;
                nextNode = malloc(sizeof(struct hashnode_s));

                nextNode->key = key;
                nextNode->tag = IntegerConst;

                nextNode->u.IntegerValue = 5;
                nextNode->next = NULL;

                if(node->next == NULL)
                {
                    // THIS IS WHERE IT CRASHES AT!
                    node->next = nextNode;
                }

                return 0;
            }
            node=node->next;
        }
    }
}

I’m trying to get rid of the segmentation fault, but I can’t any ideas?

  • 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-05-23T08:41:54+00:00Added an answer on May 23, 2026 at 8:41 am

    A few immediate issues spring to my attention when looking at your code:

    1. You always do a strcmp with StringValue even though it is in a union with IntegerValue, this means that strcmp will read an invalid memory address and cause a segfault for integer values.
    2. Is your hash-table initialized, if not then it’s quite likely that the line of code you claim is segfaulting because it is the first time a write occurs on a page without write access.
    3. You check if the next node is NULL before assigning the allocated node to be the next node, if it fails then you don’t free the memory, this memory leaking could be an issue. The obvious solution to number 1 and this would be to use the next pointer being NULL as an indicator that you’ve reached the end of the list rather than checking the string which may not always be there.
    4. The hash is a signed integer, ensure that the only negative number returned from the hashing function is -1 since any other negative could easily be another out-of-bounds access error.

    If I were you then I’d fix these other problems before trying to track down this error, it’s easier to find an error when you’re only looking for one.

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

Sidebar

Related Questions

I have a structure in C#: public struct UserInfo { public string str1 {
I have a structure: struct pkt_ { double x; double y; double alfa; double
Suppose I have the following struct: struct sampleData { int x; int y; };
I have a structure of this type (Ext 3.3.1): var page = new Ext.Viewport({
I have a structure which I need to populate and write to disk (several
I have a structure like this: <ul> <li>text1</li> <li>text2</li> <li>text3</li> </ul> How do I
I have a structure that contains an arrays of another structure, it looks something
I have a structure which I create a custom constructor to initialize the members
Here I have: Public Structure MyStruct Public Name as String Public Content as String
Currently I have a structure like this: A | +--B | +--C It's mapped

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.