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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:38:23+00:00 2026-06-03T06:38:23+00:00

I’m having a really hard time debugging this. When I try to print out

  • 0

I’m having a really hard time debugging this. When I try to print out the structures I only get the last word. Am I writing over the memory or something? Can someone help me?

typedef struct hash_table_ {
  void **order;
  int *number_next_calls;
  int *number_buckets;
  int *buckets_size;
  int *worst;
  int *total;
  float *average;
  int (*hash_func)(char *);
  data_el **buckets_array;
} hash_table, *Phash_table;

typedef struct data_{
  char *key;
  void *data;
  struct data_ *next;
}data_el;

main(){

while ((c = getchar()) != EOF) {
    if ((c == ' ') || (c == ',') || (c == '.') || (c == '!') || (c == '"') ||
        (c == ':') || (c == '\n')) {

      /* End of a word */
      if (char_index) {
        /* Word is not empty */
        word[char_index] = '\0';
        lower_case_word(word);
        if(!find_hash(dictionary,word) ){
          insert_hash(dictionary,word,frequency[hash_function(word)]);
        }
        printf("%s\n", dictionary -> buckets_array[hash_function(word)] -> key);
        printf("%d \n",hash_function(word));
        frequency[hash_function(word)]++;
        char_index = 0;
        num_words++;
      }
    }else{
      word[char_index++] = c;
    }
  }

/*This is when it prints*/
  printf("%s\n", dictionary -> buckets_array[337] -> key);
  printf("%s\n", dictionary -> buckets_array[532] -> key);
  printf("%s\n", dictionary -> buckets_array[93] -> key);

 }

 int hash_function(char *word){

  int sum,i;
  i = 0;
  sum = 0;
  while(word[i] != '\0'){
    sum = sum + word[i];
    i++;
  }
  return sum%1000;
}

void insert_hash(Phash_table table, char *key, void *data){
  int index;
  data_el *p, *cur;

  index = table -> hash_func(key);

  /*Head insertion*/
  if(table -> buckets_array[index] == NULL){
    table -> buckets_array[index] = (data_el *)malloc(sizeof(data_el));
    table -> buckets_array[index] -> data = data;
    table -> buckets_array[index] -> next =  NULL;
    table -> buckets_array[index] -> key = key;
  }else{
    printf("%s",table -> buckets_array[index] -> key);
    cur = table -> buckets_array[index];
    p = (data_el *)malloc(sizeof(data_el));
    p -> key = key;
    p -> data = data;
    p -> next = cur;
    cur = p;
    /*
    table -> buckets_array[index] = cur;
    */
  }
}
  • 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-03T06:38:24+00:00Added an answer on June 3, 2026 at 6:38 am

    In insert_hash you have

    table -> buckets_array[index] -> key = key;
    

    resp

    p -> key = key;
    

    that is, you let the bucket entry point to the same memory that got passed from main. The code is incomplete, so I can’t be sure, but I bet that it main you reuse the word array and don’t allocate a new one after each insertion. So the contents of the string pointed to by table->buckets_array[index]->key gets overwritten.

    You have to copy the strings to a new chunk of memory and let the bucket entries point to that.

    • 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
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
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 want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.