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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:50+00:00 2026-05-16T17:07:50+00:00

I would like to put a GSList inside a GHashTable , here is how

  • 0

I would like to put a GSList inside a GHashTable, here is how I managed all of this:

#include <glib.h>
#include <glib/gprintf.h>

typedef struct Foo_ {
  GHashTable * bar;
} Foo;

Foo * create() {
  Foo * foo = g_malloc(sizeof(Foo));
  foo->bar = g_hash_table_new(NULL, NULL);
  return foo;
}

void add_element(Foo * foo, gchar * key, gpointer data) {  
  GSList * list = g_hash_table_lookup(foo->bar, key);

  if(list == NULL) {
    // init empty list
    list = g_malloc(sizeof(GSList));
    list->data = NULL;
    list->next = NULL;
  }

  list = g_slist_append(list, data);
  g_hash_table_insert(foo->bar, key, list);
}


void output_content(Foo * foo, gchar * key) {
  GSList * list = g_hash_table_lookup(foo->bar, key);
  guint length = g_slist_length(list); // 
  int idx;

  for(idx = 0; idx < length; idx++) {
    int * data = g_slist_nth_data(list, idx);
    g_printf("%d\n", *data); // segfault happens here, cause data == NULL
  }
}

int main() {
  Foo * foo = create();
  int data0 = 0;
  int data1 = 1;
  int data1bis = 11;
  int data1ter = 111;
  int data2 = 2;

  add_element(foo, "data0", &data0);
  add_element(foo, "data1", &data1);
  add_element(foo, "data1", &data1bis);
  add_element(foo, "data1", &data1ter);
  add_element(foo, "data2", &data2);

  output_content(foo, "data1");

  return 0;
}

compile the program with

gcc  -g -Wall -L  -I -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  -lglib-2.0 -o main main.c

So this program generates an segfault, I don’t know how this could happen, as I corretly added all the data to the list, and the list to the hash table, Any ideas on how I could solve this ?

  • 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-16T17:07:51+00:00Added an answer on May 16, 2026 at 5:07 pm

    the next portion of code is wrong just delete it

      if(list == NULL) {
        // init empty list
        list = g_malloc(sizeof(GSList));
        list->data = NULL;
        list->next = NULL;
      }`
    

    go with this add function :

    void add_element(Foo * foo, gchar * key, gpointer data) {  
      GSList * list = g_hash_table_lookup(foo->bar, key);
    
      list = g_slist_append(list, data);
      g_hash_table_insert(foo->bar, key, list);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to put all of my ListView rows into
I would like to put a string into a byte array, but the string
I would like to put a link to a webpage in an alert dialog
I have a Window where I have put a Frame. I would like to
Using sql server 2000, I would like to take my production data and put
I would like to put my ScrollViewer in such a way that it overlaps/sits
I would like to put the date the application was built somewhere in the
I would like to put user controls on pages without registering the tag at
I would like to put a Git project on GitHub but it contains certain
I would like to put a button on top of a Google Map next

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.