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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:41:28+00:00 2026-06-12T03:41:28+00:00

I added several key, value pairs to my GLib hash table, which is under

  • 0

I added several key, value pairs to my GLib hash table, which is under my memory struct. The key is a (char *) pointer returned from a malloc. The value is the size malloc allocated.

void addstomemstruct(struct memory *mem, char *key_address, size_t sz) {
    g_hash_table_insert(mem->singleton, key_address, &sz);
    printf("** retrieving size: %Zu add=%p\n",(*(size_t*) g_hash_table_lookup(mem->singleton, key_address)), key_address);
}

Later on, when I went to retrieve the value based on the value based on the key, the returned size (value) is always 10, which was the last value I added to the hash table

size_t get_blocksize(GHashTable *hashtable, char *key_address, size_t *result_flag)
{   size_t *sz_ptr = (size_t *) g_hash_table_lookup(hashtable, key_address);
    ...

char *logmsg;asprintf(&logmsg,"In get_blocksize: *sz_ptr=%Zu\n",*sz_ptr);
    log_msg(logmsg);

printf("** retrieving size: %Zu add=%p\n",(*(size_t*) g_hash_table_lookup(hashtable, key_address)), key_address);
...
}

Printout:

preparing to add pointer: 0x9b52f10
** adding size: 1
** retrieving size: 1 add=0x9b52f10
preparing to add pointer: 0x9b52f48
** adding size: 2
** retrieving size: 2 add=0x9b52f48
preparing to add pointer: 0x9b52f58
** adding size: 3
...
preparing to add pointer: 0x9b52fc8
** adding size: 10
** retrieving size: 10 add=0x9b52fc8

then later when I try to retrieve the first value:

In get_blocksize: *sz_ptr=10
** retrieving size: 10 add=0x9b52f10
Freed address 0x9b52f10.  Size: 10. mem->stats->active_count=9, mem->stats->active_size=45.
  • 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-12T03:41:29+00:00Added an answer on June 12, 2026 at 3:41 am

    You are passing a pointer to an automatic storage duration variable (function parameter) as a hash table value. When it goes outside of the scope, it’s an undefined behavior to access it.

    You can either:

    • Allocate storage for sz with malloc/gnew and register an appropriate value_destroy_func at hash table creation.
    • Take advantage of the fact that size_t value fits in a pointer and cast it to gpointer. Technically this an UB, but should work on most platforms. Probably should not do this unless small object allocation is identified as a bottleneck.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Using Oracle) I have a table with key/value pairs like this: create table MESSAGE_INDEX
I encountered several times Key-Value pairs in the SWI-PL doc but couldn't get a
There are several questions discussing ways in which progress indication can be added to
I have a form in which several buttons are added at runtime via a
I've added several localizations to my osx applications. English Simplified Chinese Traditional Chinese Italian
I'm writing a console application in Visual Basic 2008 Express. I added several text
I have several forms added to the pages of the tab control via tabcontrol.tabpages.controls.add
I have several Core Data model version that I have added to my .xcdatamodeld
I have several ReorderLists on a page. Each list has items added to it
i just added a launch image to an application i wrote. According to several

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.