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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:34:57+00:00 2026-06-01T03:34:57+00:00

I am getting a very strange error in my code. This assignment is for

  • 0

I am getting a very strange error in my code. This assignment is for a class I’m taking and essentially we are learning how to implement a hash table. The error i’m getting is when I try and rehash to a larger size. Here’s the portion of the code giving me the problem, and I’ll explain more fully what the problem is.

if(htable->size>=htable->cap)
                    {
                        cout<<htable->cap<<endl;
                        HashTable tempht=*htable;
                        delete htable;
                        htable=new HashTable((tempht.cap * 2) + 1);



                        for (size_t i=0; i<tempht.cap; i++)
                        {

                            Node* n=tempht.table[i];
                            while (n!=NULL)
                            {
                                htable->add(n->item);
                                n=n->next;
                            }
                        }
                        if (htable->table[0]==NULL)
                        {
                            cout<<"HOORAY!"<<endl;
                        }
                    }

                    if (htable->table[0]==NULL)
                    {
                        cout<<"HOORAY!"<<endl;
                    }
                    else
                    {
                        cout<<htable->table[0]->item<<endl;
                    }

htable is a HashTable variable. In the HashTable class it contains an array Node* (Nodes are just objects I created that contain a string and a pointer to the next item in the chain). This part of the code is simply trying to rehash to a larger table. The issue I’m getting is once I exit the first if statement, my table’s first value no longer equals NULL (the test I’m running rehashes a table with nothing in it to a table that still has nothing in it, but has a larger capacity). When I run the code, the first htable->table[0]==NULL passes while the second does not, despite there being no changes other than exiting the if statement (my expected result is that the table[0] should be NULL). My best guess is it’s some kind of scoping error, but I honestly can’t see where the problem is. Any help would be greatly appreciated.

Edit: Just to clarify, the initial hash table has a capacity of 0 (this is one of the project requirements). So when i try to add an item to the table, this if statement is executed (since the size is 0 and the cap is 0, we have to maintain a load factor of 1). I can confirm that once the table reaches the first and second “Hooray” checks, that htable->cap (which is the total capacity of the array) is 1, which is what it should be. The only thing that is getting messed is bucket 0 (which in this case is the only bucket). For whatever reason, it’s null before exiting the if statement but not after.

I’m posting my whole HashTable class, let me know if you find anything.

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "Node.h"
using namespace std;
class HashTable
{
public:
    Node** table;
    int size;
    int cap;
    HashTable (int c)
    {
        size=0;
        cap=c;
        table = new Node*[cap];

        if (cap>0)
        {

            for (size_t i=0; i<cap; ++i)
            {
                table[i]=NULL;


            }
        }
    }
    ~HashTable()
    {
        delete table;
    }
    size_t hash(string thing)
    {
        size_t total=0;
        int asci;
        char c;
        size_t index;

        for (size_t i=0; i<thing.length(); i++)
        {
            total=total*31;
            c=thing[i];
            asci=int(c);

            total=asci+total;

        }

        index=total%cap;
                    cout<<"index"<<index<<endl;
            system("pause");

        return index;
    }
    void add(string thing)
    {


            size_t index;
            index=hash(thing);
                        cout<<"index "<<index<<endl;
            system("pause");
            Node* temp=table[index];
            if (temp==NULL)
            {
            cout<<"Here"<<endl;
            system("pause");
            }
            else
            {
                            cout<<"Here2"<<endl;
            system("pause");
                        cout<<"temp"<<temp->item<<endl;
            system("pause");
            }
            Node* n = new Node(thing);
            cout<<"n"<<n->item<<endl;
            system("pause");
            if (temp==NULL)
            {

                table[index]=n;
            }
            else
            {
                while (temp->next!=NULL)
                {
                    temp=temp->next;
                }
                temp->next=n;
            }

        size++;
    }
    Node* find(string search)
    {
        Node* n= NULL;
        size_t index;
        if(cap!=0)
        {
        index=hash(search);
        Node* temp=table[index];
        while (temp!=NULL)
        {
            if (temp->item==search)
            {
                n=temp;
                return n;
            }
        }
        }
        return n;
    }
    void remove (string thing)
    {
        if (find(thing)==NULL)
        {
            return;
        }
        else
        {
            size_t index;
            index=hash(thing);
            Node* temp=table[index];

            if (temp->item==thing)
            {
                table[index]=temp->next;
                delete temp;
            }
            while (temp->next!=NULL)
            {
              if (temp->next->item==thing)
              {
                  Node* temp2=temp->next;
                  temp->next=temp->next->next;
                  delete temp2;
                  break;
              }

            }
        }
        size--;
    }
    void print(ofstream &ofile)
    {

        for (size_t i=0; i<cap; i++)
        {
            Node* n=table[i];
            ofile<<"hash "<<i<<":";
            while (n!=NULL)
            {
                ofile<<" "<<n->item;
                n=n->next;
            }
        }
    }

};
  • 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-01T03:34:59+00:00Added an answer on June 1, 2026 at 3:34 am

    Well, this is C++, and I’m more a Java guy, but I’ll take a stab at it.

    Turns out the problem IS with the

                    HashTable tempht=*htable;
                    delete htable;
    

    block after all.

    See, the first line there says “copy all of the members from *htable into tempht”. So now tempht and htable SHARE their table memory, since table is just a pointer to memory that was allocated at construction, and you just copied the pointer. You wanted it to copy the nodes inside table, but it didn’t do that.

    So now you have two different HashTable objects with the same pointer value in table. Now, when tempht is freed, the destructor calls free on the table pointer, which effectively frees the table data in both objects htable and tempht.

    What you really want to do is write a copy constructor, or do something like:

    HashTable *tempht=htable;
    htable=new HashTable((tempht->cap * 2) + 1);
    for (size_t i=0; i<tempht->cap; i++)
    {
    
        Node* n=tempht->table[i];
        while (n!=NULL)
        {
            htable->add(n->item);
            n=n->next;
        }
    }
    if (htable->table[0]==NULL)
    {
        cout<<"HOORAY!"<<endl;
    }
    delete tempht;
    

    See how all I’ve really done is change tempht to a pointer, using it to point to the old hashtable while you copy all the nodes from it to the new htable object, then deleting the old Hashtable.

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

Sidebar

Related Questions

Please take a look at this code and run it: I'm getting very strange
Hello all I'm getting this very strange error: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lor g/hibernate/classic/Session; at org.cometd.hibernate.util.HibernateUtil.getSessionFactory(HibernateUt il.java:29)
Using the standard add record code I am getting a very strange error when
Very strange error I have, I am getting request as null when I try
I'm getting a very strange error with preg_replace. I'm using the following code: $text=preg_replace('/(\s)?'.preg_quote($f).'(\s)?/','<a
I am getting this error but only very occasionally. 99.9% of the time it
I am getting a very strange error message at one of our client sites
What is problem with follwoing code? I am getting very strange results. Do I
I'm using Kohana 3.1 and I'm getting a very strange error. The Kohana POST
I am extremely confused why I am getting this strange error all the sudden:

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.