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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:37:53+00:00 2026-06-07T15:37:53+00:00

I am using uthash (http://uthash.sourceforge.net/userguide.html) to use a Hash Table implementation in my C

  • 0

I am using uthash (http://uthash.sourceforge.net/userguide.html) to use a Hash Table implementation in my C Program.

I am trying to printf the data that is being added to the hash table before and after the addition and I get garbage values when I print it after the malloc. Please see the following code.

void add_user(int user_id, char *name)
{
    printf("User ID : %d Name : %s, user_id,name); // Prints fine !!
    struct my_struct *s;
    s = malloc(sizeof(struct my_struct));
    s->id = user_id;
    strcpy(s->name, name);

    printf("User ID : %d Name : %s, s->user_id,s->name); // Prints User ID fine, but for Name, only half of it is printed right, rest is Garbage !!

    HASH_ADD_INT( users, id, s );
}

If I do the following :

void add_user(int user_id, char *name)
{
    printf("User ID : %d Name : %s, user_id,name); // Prints fine !!

    struct my_struct *s;
    s = malloc(sizeof(struct my_struct));

    printf("User ID : %d Name : %s, user_id,name); // Printing same as previous printf then I get Garbage values printed for Name

}

BUT if I comment out malloc as well, then both printf statements print correctly.

HELP !!!

  • 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-07T15:37:55+00:00Added an answer on June 7, 2026 at 3:37 pm

    Gosh, I posted the question and in the next 2 mins I figured it out.

    YePHlcK, you are kinda right. However, the thing which didn’t have the memory allocated here was *name which was coming in.

    The function was called like this :

    add_user(r.user_id, r.name); // r itself is a struct where name was a pointer and not a char array.
    

    Changing name to a char array and then initializing it by using strcpy(r.name,”blah”); and then calling add solved the problem. Because strcpy would take care of allocation of memory ?!?!! Correct me if I am wrong.

    Thanks!

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

Sidebar

Related Questions

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
Using Google Maps InfoBox - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/reference.html Currently the map will scroll - pan control
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Microsoft SQL Server 2005, is there any way to see when a table
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using Rails 3.2.0 with haml, sass and coffeescript: Basically I am trying to disable

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.