我找到了調整數組大小的功能,並且無法理解它是如何工作的(或者它是否正常工作)。對於測試,我將「temp」數組設置爲一個新值,並將「startCounter」分配給該值,但startCounter的內存位置不會更改。這裏是我的代碼:調整大小數組功能不會改變內存位置
int * startCounter;
void resizeArray(int *&arraySent,int origSize,int newSize) {
output << "&arraySent " << &arraySent << endl;
output << "arraySent[0] " << arraySent[0] << endl;
int* temp = new int[newSize];
output << "&temp " << &temp << endl;
for (int i=0; i<origSize; i++) {
temp[i] = arraySent[i];
}
temp[0]=744;
delete [] arraySent;
arraySent = temp;
output << "&arraySent " << &arraySent << endl;
}
//....
startCounter = new int [3];
startCounter[0]=345;
output << &startCounter << endl;
resizeArray(startCounter,3,10);
output << "startCounter[0]" << startCounter[0] << endl;
output << "&startCounter" << &startCounter << endl;
這裏是輸出我從這個得到:
&startCounter 0x60fab8
&arraySent 0x60fab8
arraySent[0] 345
&temp 0x82cfe54
&arraySent 0x60fab8
startCounter[0] 744
&startCounter 0x60fab8
我的問題是,爲什麼startCounter的存儲位置不0x60fab8刪除它並將其分配給後更改新的「臨時」數組?現在不應該變成0x82cfe54嗎?
P.S.我瞭解載體等,但主要關注如何理解這個特定功能的工作原理。
......這是有道理的。謝謝。 – Shawn