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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:14:59+00:00 2026-05-21T17:14:59+00:00

I’m writing a program that uses a Hashtable of linked lists to count the

  • 0

I’m writing a program that uses a Hashtable of linked lists to count the frequency of words. The program will count all the words that I enter along with the frequency however after printing the hashtable, I get a segmentation fault (core dumped) error. When I valgrind my program, it shows that I get errors in three different places that are Invalid read of size 8. I’m not sure how to fix them though. Here are the three different places:

 void freeTable(HashTablePtr table) {
     int i;
     ListPtr list;

     if (table == NULL)
         return;

     for (i = 0; i < table->size; i++) {
         list = table->table[i];
         freeList(list);
     }

     free(table->table);
     free(table);
 }


 HashTablePtr createTable(int tableSize) {

     int i;
     HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr));
     table->table = (ListPtr *) malloc(sizeof(ListPtr) * tableSize);
     table->size = tableSize;

     for (i = 0; i < table->size; i++) {
         table->table[i] = createList();
     }

     return table;
 }


 void printTable(HashTablePtr table) {

     ListPtr tempList;
     NodePtr tempNode;
     HashObjectPtr obj;
     int i;

     for (i = 1; i < table->size; i++) {
         tempList = table->table[i];
         if (tempList->size != 0) {
             tempNode = tempList->head;
             obj = tempNode->HashObject;
             printf("%s\n\n", toString(obj));
         }
     }
 }

I think that the error has to due with using these lines:

tempList = table->table[i];

table->table[i] = createList();

but I’m not sure how to fix it.

Edit:

 typedef struct hashtable HashTable;
 typedef struct hashtable * HashTablePtr;

 struct hashtable {
     int size;
     ListPtr *table;
 };

Valgrind errors:

999 errors in context 5 of 9:

==73795== Invalid read of size 8

==73795== at 0x400B7D: printTable (HashTable.c:96)

==73795== by 0x400766: main (wf.c:16)

==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc’d
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 1000 errors in context 6 of 9:
==73795== Invalid read of size 8
==73795== at 0x400B2B: freeTable (HashTable.c:128)
==73795== by 0x40076E: main (wf.c:17)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc’d
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 1000 errors in context 7 of 9:
==73795== Invalid read of size 8
==73795== at 0x400D4C: createTable (HashTable.c:25)
==73795== by 0x400753: main (wf.c:14)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc’d
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)

 ListPtr createList() {
     ListPtr list;
     list = (ListPtr) malloc(sizeof(List));
     list->size = 0;
     list->head = NULL;
     list->tail = NULL;
     return list;
 }
  • 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-21T17:14:59+00:00Added an answer on May 21, 2026 at 5:14 pm

    The HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); is almost certainly wrong. You want to allocate enough storage for a HashTable, but it seems you’re allocating storage for just a pointer to a HashTable(your HashTablePtr)

    If you drop the habit of typedef’ing pointer and instead follow the approach of allocating in the following form, you won’t get into that sort of problems:

    HashTable *table = malloc(sizeof *table);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

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.