我找到了调整数组大小的功能,并且无法理解它是如何工作的(或者它是否正常工作)。对于测试,我将“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