0

我正在尝试将我的数组复制到一个更大的数组中,然后删除旧数组以进行清理。该类有一个int和一个指针,并覆盖析构函数以确保它不会在我可以删除指针时删除delete []。但我的delete []. 我= new field[1000];用来初始化数组。

我明白"Collections.exe has triggered a breakpoint."了,但断点不是我的。

inline void _resize(unsigned int newTableSize, bool _trimCalled){ 
            if (newTableSize < tableSize && _trimCalled == false) {
                _trim();
                return; 
            }
            field* newTable = new field[newTableSize]; 
            for (unsigned int x = 0; x < newTableSize; x++)
                newTable[x] = table[x];
            tableSize = newTableSize;  
            delete[] table;
            table = newTable;   

    }
inline void _trim(){
    // compact the table 
    // fill in from the end of the table
    for (int x = tableSize; !emptyPlaces.empty() ; x--){
        if (table[x].used = true){
            table[emptyPlaces.top()] = table[x];
            emptyPlaces.pop();
        }
    }
    // trim the excess
    _resize((unsigned int)(usedFields * 1.1 + 10), true);


template<typename key, typename object> class DictonaryArray {
    struct field{
        field(){ this->key = 0; this->_object = nullptr; this->used = false; }
        field(key _key, object __object){;
            key = _key;
            _object = new object();
            *_object = __object;
            this->used = true;
        }
        ~field(){ 
        }
        key key;
        object* _object;
        bool used;
    };
4

1 回答 1

1
table = newtable;; 
delete[] table;

看起来很可疑。

也许你想要,

delete[] table;
table = newTable; 

删除旧表并将新表的地址分配给它。

编辑1:

另外,假设tableSize是旧表的大小

for (unsigned int x = 0; x < newTableSize; x++)

需要是

for (unsigned int x = 0; x < tableSize; x++)

因为table[x]只能读到tableSize-1

于 2013-10-29T12:47:24.920 回答