Search code examples
c++algorithmdata-structureshashtable

Exception *read access violation* How should I properly delete array of lists of pointers?


so I have this problem with my hashtable whenever i try to delete all elements from my hashtable and then after that again add new elements to it, there will pop some exception read access violation and its caused when i add new elements to hashtable, and am pretty sure cause am clearing it wrong.My clear method looks like that:

void clear()
{

    delete[] arr;
    arr = nullptr;
    maxSize = 256;
    currentSize = 0;

}

my hashtable 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

my main:

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()
   

Much appreciate any help. (:

Reproducible Example :http://coliru.stacked-crooked.com/a/12d6d48bd423155b


Solution

  • changed clear() into this :

    void clear()
    {
    
        delete[] this->arr;
        this->maxSize = 256;
        this->arr = new list<T>[maxSize]();
        this->currentSize = 0;
    
    }
    

    now is way faster and no exceptions thank you all for feedback.