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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:13:33+00:00 2026-05-12T17:13:33+00:00

I am going to use GLib ‘s Hash table implementation in a C program

  • 0

I am going to use GLib‘s Hash table implementation in a C program and just for now
I am just experimenting with it. I wrote the following piece of code for testing:

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

 int main(){
 // Some codes and declerations here
 GHashTable *g_hash_table;
 uint32_t *a;
 a=(uint32_t *)malloc(sizeof(uint32_t));
 if(a==NULL){
    printf("Not Enough Mem For a\n");
    return 1;
 }
 *a=1123231;

 uint32_t* key;
 key=(uint32_t *)malloc(sizeof(uint32_t));
 if(key==NULL){
     printf("Not Enough Mem For key\n");
     return 1;
 }
 *key=122312312;
 int i;
 g_hash_table=g_hash_table_new(g_int_hash, g_int_equal);
 for(i=0;i<TABLE_SIZE;i++){
     *key+=1;
     *a+=1;
     g_hash_table_insert(g_hash_table,(gpointer)key,(gpointer)a);
     uint32_t *x=(uint32_t *)g_hash_table_lookup(g_hash_table,key);
     printf("Counter:%d,  %u\n",i,*x);
 }

GHashTableIter iter;
g_hash_table_iter_init(&iter,g_hash_table);
int size=g_hash_table_size(g_hash_table);
printf("First size: %d\n",size);
uint32_t *val;
uint32_t *key_;
int counter=0;

// My problem is in the following loop it 
// always returns the same and the last key value pair
 while(g_hash_table_iter_next(&iter,(gpointer*)(void*)&key_,(gpointer*)(void*)&val)){
     counter++;
     printf("%u %u\n",(uint32_t)*key_,(uint32_t)*val);
     printf("Counter: %d\n",counter);
 }
 //Some more code here        
    return 0;
}

Somehow my test code iterates correctly but in the loop it always returns the last key and last value pairs and it is always the same. What is the problem here? The above code may not run with its as it is format. I just copied and pasted some parts to give a clear idea about what I am trying to do.

  • 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-12T17:13:33+00:00Added an answer on May 12, 2026 at 5:13 pm

    There is an error in key, a declarations. You always put the same pointer in the hash table. Try:

    #include <glib.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    
    #define TABLE_SIZE 12
    
    int main() {
        // Some codes and declarations here
        GHashTable *g_hash_table;
        int i;
    
        g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
        for (i=0; i<TABLE_SIZE; i++)
        {
            uint32_t* key = (uint32_t *)malloc(sizeof(uint32_t));
            uint32_t* a = (uint32_t *)malloc(sizeof(uint32_t));
            *key = i;
            *a   = i+10;
            g_hash_table_insert(g_hash_table, (gpointer)key, (gpointer)a);
            uint32_t *x = (uint32_t *)g_hash_table_lookup(g_hash_table,key);
            printf("key: %d -->  %u\n", *key ,*x);
        }
    
        GHashTableIter iter;
        int size=g_hash_table_size(g_hash_table);
        printf("First size: %d\n", size);
    
        uint32_t *val;
        uint32_t *key_;
    
        // My problem is in the following loop
        // it always returns the same and the last key value pair
    
        g_hash_table_iter_init (&iter, g_hash_table);
        while (g_hash_table_iter_next (&iter, (gpointer) &key_, (gpointer) &val))
        {
            printf("key %u ---> %u\n", (uint32_t)*key_, (uint32_t)*val);
        }
    
        // TODO: free keys
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to use an UINavigationController to keep track of all the views
I am going to use Flask micro-framework (based on Werkzeug , and I'll be
I was going to use varchar(20) , but I was wondering what should if
I am going to use NSHTTPCookieStorage in an iOS App to manage cookies that
I am going to use JQuery UI Tabs Plugin on my web application. And
We are going to use the Sitecore CMS in our new project. We'd like
In a upcoming project I'm going to use NHibernate with Oracle. I have used
I have a struct where a constant char string is defined and a pointer
Recently, I made a new friend. His name is _expand , and we've had
I need to get all the Django request headers. From what I've read, Django

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.