Properly removing elements from linked-list? Memory errors with pointers


I'm implementing a hashtable that has a remove_entry function as well as a clear_table function. Right now I'm getting memory read errors pertaining to the remove_entry function. And help would be greatly appreciated

These are my structures:

typedef struct bucket { char *key; void *value; struct bucket *next; } Bucket; typedef struct { int key_count; int table_size; void (*free_value)(void *); Bucket **buckets; } Table;

Here's my function:

int remove_entry(Table * table, const char *key){ unsigned int hc = 0; Bucket *curr_b; Bucket *next_b; if(table == NULL || key == NULL){ return FAIL; } else { hc = hash_code(key)%(table->table_size); if(table->buckets[hc] != NULL){ curr_b = table->buckets[hc]; /*Check the buckets in linked list*/ while(curr_b != NULL){ next_b = curr_b->next; if(strcmp(curr_b->key,key) == 0){ free(curr_b->key); if(table->free_value != NULL){ table->free_value(curr_b->value); } free(curr_b); curr_b = NULL; table->key_count--; return SUCC; } else { curr_b = next_b; } } return FAIL; } return FAIL; } }

The memory leaks come after removing an entry, and trying to read the table after. I don't think I removed things right.

Memory errors:

Can't figure out how to copy/paste from terminal, so they all say things like

Invalid read of size __ Address ____ is __ bytes inside a block of size ___ free'd


You need to consider the case where table->buckets[hc] is the bucket you free.

Right before free(curr_b->key); add:

if(curr_b == table->buckets[hc]) { table->buckets[hc] = next_b; }

As it is now, you free a bucket, but table->buckets[hc] still points to it. So the next time you read it you are reading free'ed memory. Thus the error.

Also, you need to keep track of the <em>previous</em> bucket so you can point the previous bucket to the next bucket when you remove a bucket.


  • Insert data-bind values inside textarea as a default canned message using angular
  • scanf validation sits and waits for another input. Why?
  • How to add an integer unique id to query results - __efficiently__?
  • Change storage class of (existing) objects in Google Cloud Storage
  • Failed to start an instrument test from Firebase gcloud command line
  • Amazon s3 – 403 Forbidden with Correct Bucket Policy
  • Weighted round robin dns between 2 Cloudfront distributions
  • Can I monitor the progress of an S3 download using the AWS SDK?
  • Google datalab : how to import pickle
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Word Open XML Mail Merge
  • Replace value with Factor in r data.table
  • Database structure design with variable amounts of fields
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Submit form in a displaytag pagination
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • Eraser for UIBezierPath
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • AngularJs get employee from factory
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?