异常 *读取访问冲突* 我应该如何正确删除指针列表数组?
Exception *read access violation* How should I properly delete array of lists of pointers?
所以每当我尝试从我的哈希表中删除所有元素然后再次向它添加新元素时,我的哈希表就会出现这个问题,会弹出一些异常读取访问冲突 它是在我向哈希表添加新元素时引起的,我很确定是因为我正在清除它 wrong.My 清除方法看起来像这样:
void clear()
{
delete[] arr;
arr = nullptr;
maxSize = 256;
currentSize = 0;
}
我的哈希表class:
template<class T>
class hashTable {
private:
unsigned int currentSize;
unsigned int maxSize;
public:
list<T>* arr;
hashTable() {
maxSize = 256;
currentSize = 0;
arr = new list<T>[maxSize];
}
//some interface methods
我的主要:
hashTable<int>* table = new hashTable<int>();
const int MAX_ORDER = 6;
for (int o = 1; o <= MAX_ORDER; o++)
{
const int n = pow(10, o);
for (int i = 0; i < n; i++)
{
//add element
}
table->clear()
非常感谢任何帮助。 (:
将 clear() 更改为:
void clear()
{
delete[] this->arr;
this->maxSize = 256;
this->arr = new list<T>[maxSize]();
this->currentSize = 0;
}
现在速度更快,无一例外,感谢大家的反馈。
所以每当我尝试从我的哈希表中删除所有元素然后再次向它添加新元素时,我的哈希表就会出现这个问题,会弹出一些异常读取访问冲突 它是在我向哈希表添加新元素时引起的,我很确定是因为我正在清除它 wrong.My 清除方法看起来像这样:
void clear()
{
delete[] arr;
arr = nullptr;
maxSize = 256;
currentSize = 0;
}
我的哈希表class:
template<class T>
class hashTable {
private:
unsigned int currentSize;
unsigned int maxSize;
public:
list<T>* arr;
hashTable() {
maxSize = 256;
currentSize = 0;
arr = new list<T>[maxSize];
}
//some interface methods
我的主要:
hashTable<int>* table = new hashTable<int>();
const int MAX_ORDER = 6;
for (int o = 1; o <= MAX_ORDER; o++)
{
const int n = pow(10, o);
for (int i = 0; i < n; i++)
{
//add element
}
table->clear()
非常感谢任何帮助。 (:
将 clear() 更改为:
void clear()
{
delete[] this->arr;
this->maxSize = 256;
this->arr = new list<T>[maxSize]();
this->currentSize = 0;
}
现在速度更快,无一例外,感谢大家的反馈。