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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:36:21+00:00 2026-06-15T18:36:21+00:00

So I wrote a simple Hash class, and when using the visual studio environment

  • 0

So I wrote a simple Hash class, and when using the visual studio environment I get a debug assertion error “Expression: String subscript out of range” However when I use c++ Linker on the command prompt to compile and run the program runs fine. The error occurs at h.add(“a”, “la”);

Here is the code

#include <iostream>
#include <string>
using namespace std;
bool die(const string &msg);

class Hash{
    public:
        Hash( unsigned tablesize, unsigned maxUsed );
        ~Hash();
        bool in( const string & code ) const;
        bool getDescription( string & description, const string & code ) const;
        void add( const string & code, const string & description );
        void changeDescription( const string & code, const string & newDescription );
        void showone(const string &code) const;
        void show() const;
    private:
        struct Data{
            string code;
            string descrip;
        };
        unsigned hash(unsigned val) const;
        unsigned rehash(unsigned val) const;
        static unsigned Hash::partialHash( const string & code );
        static bool prime( unsigned n );
        unsigned findindex(const string &code) const;
        Data *ptr;
        unsigned maxUsed;
        unsigned elements;
        unsigned tablesize;
        unsigned p;
        unsigned p2;
};

Hash::Hash(unsigned size, unsigned maxused){
    if(UINT_MAX-3<size || size<=4 || size<=maxused )
        die("Invalid Constructor");
    for(tablesize=size; !prime(tablesize); tablesize++){}
    ptr=new Data[tablesize];
    elements=0;
    maxUsed=maxused;
    for(p=tablesize; !prime(--p);){}
    for(p2=p; !prime(--p2);){}
    for(unsigned i=0; i<tablesize; i++){
        ptr[i].code="\0";
        ptr[i].descrip="\0";
    }
}

Hash::~Hash(){
    delete[] ptr;
}

bool Hash::in(const string &code)const{
    if(code==ptr[findindex(code)].code)
        return true;
    return false;
}

void Hash::showone(const string &code) const{
 unsigned i=findindex(code);
 cout<<'['<<i<<"]: "<<ptr[i].code<<' '<<ptr[i].descrip<<'\n';
}
void Hash::show() const{
 for(unsigned i=0; i<tablesize; i++)
  cout<<'['<<i<<"]: "<<ptr[i].code<<' '<<ptr[i].descrip<<'\n';
}

bool Hash::getDescription( string & description, const string & code ) const{
    if(in(code)){
        description=ptr[findindex(code)].descrip;
        return true;
    }
    return false;
}

void Hash::changeDescription(const string & code, const string & newdescription ){
    if(in(code)){
        ptr[findindex(code)].descrip=newdescription;
    }else{
        die("code not in table");
    }
}

unsigned Hash::hash(unsigned partialHashValue)const{
    return partialHashValue%p;
}

unsigned Hash::rehash(unsigned partialHashValue)const{
    return partialHashValue%p2+1;
}

unsigned Hash::partialHash( const string & code ){
    return (code[0]*26+code[1])*26+code[2];
}

void Hash::add( const string & code, const string & description ){
    if(in(code)) die("can't add");
    if(elements==maxUsed) die("Overflow");
    unsigned i=findindex(code);
    ptr[i].code=code;
    ptr[i].descrip=description;
    elements++;
}

bool Hash::prime( unsigned n ){
        if( n < 4 )  return n > 1;
        if( n%2 == 0 || n%3 == 0 )  return false;
        for(  unsigned fac = 5, inc = 4;  ;  fac += inc = 6-inc  ){
            if( fac > n/fac )  return true;
            if( n%fac == 0 )  return false;
        }   
    }

unsigned Hash::findindex( const string &code) const{
    unsigned partial=partialHash(code);
    unsigned hashnum = hash(partial);
    if(ptr[hashnum].code=="\0" || ptr[hashnum].code == code) return hashnum;
    unsigned rehashnum = rehash(partial);
    do{
        hashnum = (hashnum + rehashnum) % tablesize;
    }while(  ptr[hashnum].code != "\0" && ptr[hashnum].code != code );
    return hashnum;
}

int main(){
    Hash h(12, 8);
    h.add("LAX", "Space Shuttle Endeavour arrived here 9/21/2012");
    h.add("DEN", "jajaja");
    h.add("gold", "lalalala");
    h.add("Pp", "la");
    h.add("a", "la");
    h.add("b", "la");
    h.add("c", "la");
    h.add("d", "la");
    cout<<"p\n";
    h.showone("LAX");
    cout<<"\n\n";
    h.show();
}

bool die(string const &msg){
 cerr<<"fatal error: "<<msg;
 exit(EXIT_FAILURE);
}

Here is teh output

[0]: gold lalalala
[1]: DEN jajaja
[2]: LAX Space Shuttle Endeavour arrived here 9/21/2012
[3]:
[4]:
[5]: a la
[6]: b la
[7]: Pp la
[8]:
[9]: d la
[10]: c la
[11]:
[12]:
  • 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-15T18:36:22+00:00Added an answer on June 15, 2026 at 6:36 pm

    In partialHash funciton, code[2] is out-of-boundary access when code only contains “a”

    unsigned Hash::partialHash( const string & code ){
        return (code[0]*26+code[1])*26+code[2];
    }
    

    you could use at() function and catch the thrown exception

       unsigned Hash::partialHash( const string & code )
        {
            try {
              return (code.at(0)*26+code.at(1))*26+code.at(2);
            } 
            catch(std::exception& e){
              std::cout << e.what() << std::endl;
            }
            return 0;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote simple class that on the start it just increase the value of
I wrote a simple plugin which sets some css code using wp_options. It all
I'm in the middle of writing a simple hash function which get the input
I was comparing a simple hash function that I wrote which just multiplies it
I wrote this simple function once, to display notifications on hash change : function
I'm doing a simple Lookbehind Assertion to get a segment of the URL (example
I'm trying to make simple library blake hash function wrapper using python ctypes from
i wrote simple 3 functions to scrape titles , description and keywords of simple
I wrote simple load testing tool for testing performance of Java modules. One problem
I wrote a simple XML file and a DTD file including an entity, 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.