so I have exception while I try to rehash hashtable second time
here is my rehash Method.
void ReHash()
{
if (currentSize >= maxSize * 0.8) {
int tempSize = maxSize * 2;
cout << "rehash" << endl;
list<T>* tempArr = new list<T>[tempSize];
Node<T>* temp = NULL;
int index = 0;
for (int i = 0; i < maxSize - 1; i++)
{
if (arr[i].getSize() > 0) {
for (int j = 0; j < arr[i].getSize(); j++) {
temp = arr[i].getDataFromSpecificNode(j);
if (temp != NULL) {
index = hashFunction(temp->key);
tempArr[index].addAtTail( temp->getData(), temp->key);
break;
}
}
}
}
delete[] arr;
maxSize = maxSize * 2;
arr = new list<T>[maxSize];
arr = tempArr;
delete[] tempArr;
}
}
I'm pretty sure that rehash method is the one that causes problems. Thats why I would like to ask if am deleting smth wrong?
here some bigger sample if needed: http://coliru.stacked-crooked.com/a/d1bd6308849f153e
thank you for any Feedback.
You're both creating and deleting something wrong.
First, you create one array,
list<T>* tempArr = new list<T>[tempSize]
then you create another one,
arr = new list<T>[maxSize];
which you immediately throw away (leak) by pointing to the first array you created instead,
arr = tempArr;
and lastly, you make sure that arr
is an invalid pointer by deleting what it points to ( arr
and tempArr
have the same value at this point),
delete[] tempArr;
There should be one new
- for the new array - and one delete []
, for the old array.
At the end of the function, you need to release your previous array:
delete [] arr;
then "take over" the first array you created:
arr = tempArr;
and adjust your size:
maxSize = maxSize * 2;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.