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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:12:19+00:00 2026-06-15T06:12:19+00:00

So I’m having a problem where I have an array of char*s (declared as)

  • 0

So I’m having a problem where I have an array of char*s (declared as) char** where the array is dynamically allocated (by calloc), but the char*s within it are static.

This works fine for me until I attempt to free the array (during resizing), at which point I get

*** glibc detected *** ./hash_table_test: free(): invalid next size (normal): 0x0891f028 ***

I tried setting all of the ptrs in the array to NULL, but then I got the error

*** glibc detected *** ./hash_table_test: double free or corruption (!prev): 0x0815b028 ***

Here is the relevant code:

Table struct:

struct string_hash_table {

//Array of c-strings
char** table;
//number of elements in the table
int num_elements;
//size of table
int table_size;

//Primes
int *primes;
//Current position in primes array
int primes_index;
//the size of the primes array
int primes_size;
};
//TypeDefs--------------------------------
typedef struct string_hash_table HashTable;

Rehash Function (source of errors)

void rehash_string(HashTable *table) {

int prev_size = table->table_size;
int i;

table->table_size = table->table_size * 2;

//create new array
char** new_table = calloc(table->table_size, sizeof(char*));
printf("new table created\n");
int index;
printf("before loop prev_size is %d\n", prev_size);
//add all elements to new_table
for (i = 0; i < prev_size; i++) {
    printf("on %d\n", i);
    index = find_spot_string(new_table, table->table_size, table->table[i]);
    printf("after find_spot_string\n");
    if (index != -1) {
        table->table[index] = table->table[i];
    }
}

//free and swap
printf("before free\n");
empty_string_array(table->table, table->table_size);
free(table->table);
table->table = new_table;

Initialization of HashTable struct:

//Takes a HashTable and initializes it
void init_hash_table(HashTable *table) {

table->primes_index = 0;
table->num_elements = 0;
table->primes_size = 297;
table->primes = prime_list;

table->table_size = table->primes[0];
table->table = calloc(table->table_size, sizeof(char*));
}

declarations of static strings within:

    char* temp = "hello";
add_hash_table_string(table, temp);

temp = "luck";
add_hash_table_string(table, temp);

temp = "stuck";
add_hash_table_string(table, temp);

temp = "buck";
add_hash_table_string(table, temp);

temp = "muck";
add_hash_table_string(table, temp);

temp = "much";
add_hash_table_string(table, temp);

Currently I’m just testing my code here, everything works except for the rehashing function above. Anyone have any ideas? or leads I should follow?

EDIT: adding code for add_hash_table_string

void add_hash_table_string(HashTable *table, char* element) {

    //if non-null element, and element is not in the HashTable
    if (element != NULL && contains_hash_table_string(table, element) == 1) {

        //if the table is full
        if (table->table_size / 2 < table->num_elements) {

            rehash_string(table);
        }

        int index = find_spot_string(table->table, table->table_size, element);

        table->table[index] = element;
        table->num_elements++;
    }
}

EDIT2:

forgot to be precise, the error is occurring at the line with free(table->table) in the rehash function

  • 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-15T06:12:20+00:00Added an answer on June 15, 2026 at 6:12 am

    One possible problem, you free the old table with the new size

    empty_string_array(table->table, table->table_size);
    

    Another one might be

    index = find_spot_string(new_table, table->table_size, table->table[i]);
    printf("after find_spot_string\n");
    if (index != -1) {
        table->table[index] = table->table[i];
    

    If this is supposed to copy the entries to the new_table, it doesn’t AFAICS. When index is greater than prev_size, you write beyond the end of table.

    • 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&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I have been unable to fix a problem with Java Unicode and encoding. The

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.