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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:30:42+00:00 2026-06-12T09:30:42+00:00

I’m using a malloc’d string as a key to a GLib hash table. I

  • 0

I’m using a malloc’d string as a key to a GLib hash table. I then have several updates to make. Each update uses a newly malloced string that is identical in the actual sequence of characters. If I do an an insert, will the old string be overwritten? How can I make sure to free it so I don’t keep extra copies around? If it’s not overwritten, how can I make sure to implement an update without it being too slow?

  • 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-12T09:30:42+00:00Added an answer on June 12, 2026 at 9:30 am

    If you supply the correct comparator and key/value destructors to g_hash_table_new_full when creating the hash table, you don’t have to do anything extra. The table will understand that the key strings are the same even if they are separate copies, and it will automatically free repeated copies of the same key as well.

    Here is an example (the ugly printfs are only there to show whats happening):

    First you write a destructor:

    void
    free_data (gpointer data)
    {
      printf ("freeing: %s %p\n", (char *) data, data);
      free (data);
    }
    

    Then you instantiate and play around with the GHashTable:

    int main (void)
    {
      char *key, *val;
      GHashTable *hash_table
      = g_hash_table_new_full (g_str_hash,  /* Hash function  */
                               g_str_equal, /* Comparator     */
                               free_data,   /* Key destructor */
                               free_data);  /* Val destructor */
      /* Insert 'k_1' */
      key = strdup ("k_1");
      printf ("%s %p\n", key, key);
      val = strdup ("v_1");
      printf ("%s %p\n", val, val);
    
      printf ("inserting\n");
      g_hash_table_insert (hash_table, key, val);
      printf ("insert finished\n");
    
      /* Insert 'k_1' again using new strings */
      key = strdup ("k_1");
      printf ("%s %p\n", key, key);
      val = strdup ("new_v_1");
      printf ("%s %p\n", val, val);
    
      printf ("inserting\n");
      g_hash_table_insert (hash_table, key, val);
      printf ("insert finished\n");
    
      g_hash_table_unref (hash_table);
    
      return 0;
    }
    

    Here is what a test run looks like:

    k_1 0x80cce70
    v_1 0x80cce80
    inserting
    insert finished
    k_1 0x80cce90
    new_v_1 0x80ccea0
    inserting
    freeing: k_1 0x80cce90
    freeing: v_1 0x80cce80
    insert finished
    freeing: k_1 0x80cce70
    freeing: new_v_1 0x80ccea0
    

    You will see that if you create a ‘new’ key identical to an existing key, and try to insert a corresponding ‘new’ value, then the ‘insert’ operation automatically destroys the ‘new’ key (because it is the same as the existing one), as well as the old value (because it has to be replaced with the new one).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.