Error "read access violation" appears randomly
-
Hello
I was making my hashtable visual project based on linear probing and got some issue.
I have a variable N,which was declared privately,that describes how many elements in hashtable, then i have a method **put **,where i get problem when incrementing N(N++) exactly after or in more put after resizing hashtable with method resize(I think they are connected with this issue may be?) .In debugger it is "Exception at 0x5028bff6, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)" and variable N,as i said,is 2 or 3(N=2,N=3). I would appreciate any help.Added:Hmm.. if i made this process manually(putting elements) and initialize with N=0 in constr then the same exception appears when N=2 in String 18 of method resize : temp->put(keys[i],vals[i]);
void put(QString key,QString val) { if (val==NULL) { deleteKey(key); return; } **if (N>=M/2) resize(2*M);** int i; for (i=(qHash(key) & 0x7fffffff) % M;keys[i]!=NULL;i=(i+1) % M) { if (QString::compare(keys[i],key)==0) {vals[i]=val; return;} } keys[i]=key; vals[i]=val; N++; //here is exception } void resize(int capacity) { LinearProbingHashST *temp=new LinearProbingHashST; temp->init(capacity); for (int i=0;i<M;i++) { if (keys[i]!=NULL){ temp->put(this->keys[i],this->vals[i]); } } keys=temp->keys; keys=temp->vals; M=temp->M; delete temp; } public: LinearProbingHashST() { init(INIT_CAPACITY); } void init(int capacity) { M=capacity; keys=new QString[M]; vals=new QString[M]; N=0; }
-
Hi and welcome to devnet,
Do you ensure that M has a correct value ? How do you do the resize ?
-
@CyxouD said:
keys=temp->keys; keys=temp->vals;
Above code seems contains undesired features.
In any case such code complexity requires compilable simple test case.