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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:36:02+00:00 2026-05-24T00:36:02+00:00

Whew! Long title…here’s some pseudo-code to explain that verbiage: int main(){ int* ptr =

  • 0

Whew! Long title…here’s some pseudo-code to explain that verbiage:

int main(){
int* ptr = function1(); //the data that ptr points to is correct here
function2(ptr);
}

int function2(int* ptr){
//the data that ptr points to is still correct
int i;
for(i=0;i<length;printf("%d\n", (*ptr)[i]), i++); //since ptr points to a contiguous block of memory
function3(ptr);
}

int function3(int* ptr){
//the data that ptr points to is INCORRECT!!!
}

Why would the data in function3 be incorrect?

Note: function1 performs a malloc() and returns the pointer to that memory.

ACTUAL CODE

#include <stdlib.h>
#include <stdio.h>

//Structures
struct hash_table_data_
{
  int key, data;
  struct hash_table_data_ *next, *prev;
};

struct hash_table_
{
  int num_entries;
  struct hash_table_data_ **entries;
};

typedef struct hash_table_data_ hash_table_data;
typedef struct hash_table_ hash_table;

//Prototypes
hash_table *new_hash_table(int num_entries);
int hash_table_add(hash_table *ht, int key, int data);
int hash_table_loader(hash_table* ht);

//Main
int main()
{
  int num_entries = 8;//THIS MUST BE AUTOMATED

  hash_table* ht = new_hash_table(num_entries);
  hash_table_loader(ht);

  return 0;
}

//Function Definitions
hash_table *new_hash_table(int num_entries)
{
  hash_table* ht = (hash_table*) malloc(sizeof(hash_table));

  hash_table_data* array = malloc(num_entries * sizeof(hash_table_data));
  int i;
  for (i=0;i<num_entries;i++)
    {
      array[i].key = -1;
      array[i].data = -1;
      array[i].next = NULL;
      array[i].prev = NULL;
    }

  ht->entries = &array;
  ht->num_entries = num_entries;

  return ht;
}

int hash_table_add(hash_table *ht, int key, int data)
{
  //VERIFY THAT THE VALUE ISN'T ALREADY IN THE TABLE!!!!!!!!!!!
  int num_entries = ht->num_entries;
  hash_table_data* array = *(ht->entries); //array elements are the LL base
  int hash_val = key%num_entries;

  printf("adding an element now...\n");
  printf("current key: %d\n", array[hash_val].key);

  int i;
  for(i=0;i<num_entries;printf("%d\n", (*(ht->entries))[i].key),i++);//DATA IS INCORRECT!!!!

  if (array[hash_val].key == -1)//is this the base link?
    {
      printf("added a new base link!\n");
      array[hash_val].key = key;
      array[hash_val].data = data;
      array[hash_val].next = NULL;
      array[hash_val].prev = &(array[hash_val]);
    }
  else//since it's not the base link...do stuff
    {
      hash_table_data* new_link = malloc(sizeof(hash_table_data));
      new_link->key = key;//set the key value
      new_link->data = data;//set the data value
      if (array[hash_val].next == NULL)//we must have the second link
    {
      printf("added a new second link!\n");
      new_link->prev = &(array[hash_val]); //set the new link's previous to be the base link
      array[hash_val].next = new_link; //set the first link's next
    }
      else//we have the 3rd or greater link
    {
      printf("added a new 3rd or greater link!\n");
      hash_table_data next_link_val = *(array[hash_val].next);
      while (next_link_val.next != NULL)//follow the links until we reach the last link
        {
          next_link_val = *(next_link_val.next);//follow the current link to the next
        }
      //now that we've reached the last link, link it to the new_link
      next_link_val.next = new_link; //link the last link to the new link
      new_link->prev = &(next_link_val); //link the new link to the last link
    }
    }

  return 0;
}

int hash_table_loader(hash_table* ht)
{
  int i;
  for(i=0;i<(ht->num_entries);printf("%d\n", (*(ht->entries))[i].key),i++); //DATA IS STILL CORRECT HERE

  FILE *infile;
  infile = fopen("input.txt", "r");
  while(!feof(infile))
    {
      int key,data;
      fscanf(infile, "%d %d", &key, &data);
      hash_table_add(ht, key, data);
    }
  fclose(infile);
}

Note: Issue occurring the first time hash_table_add() is called.

  • 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-24T00:36:03+00:00Added an answer on May 24, 2026 at 12:36 am

    Your first problem is here:

    ht->entries = &array;

    You cause the structure to hold a hash_table_data** which points to the variable hash_table_data* array which is local to the function; then you exit the function and return a pointer to the structure. The structure still exists (it was allocated via malloc(), and the stuff that array points to still exists, but array itself does not. Accordingly, this pointer within the structure is now invalid.

    As far as I can tell, there is no reason for you to be holding a pointer-to-pointer here. Just use hash_table_data* as the entries type, and copy array into that struct member. Pointers are values too.

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

Sidebar

Related Questions

No related questions found

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.