[英]Copying Arrays and cleaning up
我正在嘗試將陣列復制到更大的陣列中,然后刪除舊的陣列進行清理。 該類具有一個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;
};
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.