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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:45:58+00:00 2026-05-28T03:45:58+00:00

The documentation for g_hash_table_new() indicates Hash values are used to determine where keys are

  • 0

The documentation for g_hash_table_new() indicates

Hash values are used to determine where keys are stored within the GHashTable data structure.

but how are the hash values used?

It seems g_hash_table_foreach() traverses the table from 0 to N- 1 for N nodes. I used this function to print out the hash, key, and value for each node:

test.c

#include <glib.h>

static GHashTable* _htab = NULL;

static int VALS[] = { 1, 2, 3, 4, 5 };

struct kv {
  gpointer key;
  gpointer val;
};

static struct kv KEYVALS[] = {
  { "aaa",    &VALS[0] },
  { "a",      &VALS[1] },
  { "b",      &VALS[2] },
  { "bbbbbb", &VALS[3] },
  { "aaaaa",  &VALS[4] }
};

static void iter(gpointer key, gpointer val, gpointer data) {
  g_printf("key=%s(%p) val=%d(%p) hash=%u\n",
      (char*)key, key,
      *(int*)val, val,
      g_str_hash((char*)key)
  );
}

int main(int argc, char* argv[]) {
  int i;

  if (!_htab) {
    _htab = g_hash_table_new(g_str_hash, g_str_equal);
    g_assert(_htab);
  }

  for (i = 0; i < sizeof(KEYVALS) / sizeof(KEYVALS[0]); i++) {
    g_hash_table_insert(_htab, KEYVALS[i].key, KEYVALS[i].val);
  }
  g_hash_table_foreach(_htab, iter, NULL);
  g_hash_table_remove_all(_htab);
  g_hash_table_destroy(_htab);
  return 0;
}

The output is always the same (other than the pointer values) even after multiple runs, so there’s some kind of algorithm used here. How does a node’s hash (e.g., from g_str_hash()) determine where it’s stored in the GHashTable?

$ gcc `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` test.c
$ ./a.out
key=bbbbbb(0x10f142ee0) val=4(0x10f1430ac) hash=4087176913
key=a(0x10f142edc) val=2(0x10f1430a4) hash=177670
key=b(0x10f142ede) val=3(0x10f1430a8) hash=177671
key=aaaaa(0x10f142ee7) val=5(0x10f1430b0) hash=252781386
key=aaa(0x10f142ed8) val=1(0x10f1430a0) hash=193485928
$
  • 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-28T03:45:59+00:00Added an answer on May 28, 2026 at 3:45 am

    Let’s see what the source code might tell us.

    static inline guint
    g_hash_table_lookup_node (GHashTable    *hash_table,
                              gconstpointer  key,
                              guint         *hash_return)
    {
      /* ... */
      hash_value = hash_table->hash_func (key);
      /* ... */
      node_index = hash_value % hash_table->mod;
      /* ... */
    

    The hash value is taken modulo some value. Let’s see what it might be.

    static void
    g_hash_table_set_shift (GHashTable *hash_table, gint shift)
    {
      /* ... */
      hash_table->size = 1 << shift;
      hash_table->mod  = prime_mod [shift];
      /* ... */
    

    Aha, a prime number from a static const array of predefined primes. But where is it set?

    #define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
    
    /* ... */
    
    GHashTable *
    g_hash_table_new_full (GHashFunc      hash_func,
                           GEqualFunc     key_equal_func,
                           GDestroyNotify key_destroy_func,
                           GDestroyNotify value_destroy_func)
    {
      /* ... */
      hash_table = g_slice_new (GHashTable);
      g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
    

    and

    static void
    g_hash_table_resize (GHashTable *hash_table)
    {
      /* ... */
      g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
    

    So on GHashTable’s creation a certain prime number is assigned to it and it’s used to translate a hash value to an index. Afterwards, when the hash table grows, g_hash_table_set_shift is called once again to update the prime value and causes different indices to be returned for given hash values.

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

Sidebar

Related Questions

I can't seem to find the documentation explaining how to create a hash table
The documentation for -hash says it must not change while a mutable object is
Per the Java documentation, the hash code for a String object is computed as:
The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object
Documentation is severely lacking on anything to do with stored procedures in mysql with
I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I'm unable to find any documentation on how to merge two hash maps. This
From MSDN documentation, it seems as both GetHashCode() and Equals() haven't been overriden in
on this page: http://www.shutterfly.com/documentation/OflyCallSignature.sfly it says once you generate a hash you then: convert
According to the Python documentation, only a few hash algorithms are guaranteed to be

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.