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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:22:51+00:00 2026-05-29T05:22:51+00:00

I have a program to dynamically increase the capacity of a hashtable when the

  • 0

I have a program to dynamically increase the capacity of a hashtable when the size of any
of its buckets goes beyond a maximum value. However i am being hit with a “* glibc
detected *
realloc(): ” error when i try to run my code.

Could anyone help me out with it? Sorry for positing so much code here,but i really need
the help.

  /* structure for the hashtable containing an array of nodes */

  typedef struct Hashtable hashtable;
  struct Hashtable {
  struct node **list;
  };   


  /* the function which resizes the hashtable and re-arranges the values according to 
     new capacity */

  void reSize(hashtable *h)
   {

    int i;
    node **newlist=realloc(h->list,2*capacity*sizeof(node*));
    h->list=newlist;
   int temp=capacity,index;
   capacity=capacity * 2;
    for(i=temp;i<capacity;i++)
    h->list[i]=calloc(1,sizeof(node));
    }


    /* mystructure init */
    struct hashtable *mystruct_init()
    {
     int i;      
     hashtable *hashtable =calloc(1, sizeof(*hashtable));

     // loop through and allocate memory for each element in list
     hashtable->list= calloc(1,sizeof(node *));
      for (i = 0; i < 16; i++) {
       hashtable->list[i] = calloc(1, sizeof(node));
    }

   return hashtable;
    } 
   /* in my main function */  

    hashtable *h1=(hashtable *) mystruct_init();
    hashtable *h2=(hashtable *) mystruct_init();

I am getting this “* glibc detected * ./compareDocs: realloc(): ” error when i try
to run it. Could someone point out as to where i am going wrong in my code?? I’ve spend
a whole night trying to debug this thing, so any help would be really nice. Sorry for
posting so many lines of code..

  • 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-29T05:22:52+00:00Added an answer on May 29, 2026 at 5:22 am

    In your mystruct_init() function you’ve allocated only one node * for your your list:

    hashtable->list= calloc(1,sizeof(node *));
    

    And then went on to dereference elements past the end of the allocated memory:

    for (i = 0; i < 16; i++) {
       hashtable->list[i] = calloc(1, sizeof(node));
    

    Also, in your reSize() function you use the variable capacity but that does not appear to be defined anywhere. Is this your real code? And if it is, what is the value of capacity?

    Edit: You should probably make the code in your init function look like this:

    struct hashtable *mystruct_init()
    {
        int i;      
        hashtable *hashtable =calloc(1, sizeof(*hashtable));
    
        // loop through and allocate memory for each element in list
        hashtable->list= calloc(capacity, sizeof(node *));
        for (i = 0; i < capacity; i++) {
           hashtable->list[i] = calloc(1, sizeof(node));
        }
        return hashtable;
    }
    

    Notice that I’ve used capacity in the call to calloc() and as the controlling value in the following for loop.

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

Sidebar

Related Questions

I have a flash program that loads movie clips dynamically and sometimes they want
I have a Flash program that loads AS2 and AS3 SWF files dynamically with
I have program that has a variable that should never change. However, somehow, it
My program have several worker threads that calling a function in a dynamically loaded
I'm writing a program consisting of dynamically created panels that each have a few
I have a program that dynamically creates controls when it starts, it works just
Have a program that's dynamically generating an Excel file and a csv. The excel
Possible Duplicate: C dynamically growing array I have a program and I need to
I have a program which will dynamically set an iframe src to load pages.
I have coded a program which dynamically inserts nodes and edges, overlaying the graph

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.