指针数组无法解释的行为(带浅拷贝)c++; 这是一个C++中的指针数组(GNU-GCC编译器),我找不到解释,希望有人能消除混淆。
我正在创建一个指针数组(arr_ptr),指针指向有效数据,然后我创建另一个指针数组(arrcopy),我做——我认为——一个浅拷贝(arrcopy=arr_ptr),我得到了预期的数据……到目前为止还不错 我不明白的是,在我删除arr_ptr后,arrcopy不应该仍然指向我的有效数据吗?为什么没有发生这种情况? 多谢各位指针数组无法解释的行为(带浅拷贝)c++; 这是一个C++中的指针数组(GNU-GCC编译器),我找不到解释,希望有人能消除混淆。,c++,arrays,pointers,C++,Arrays,Pointers,我正在创建一个指针数组(arr_ptr),指针指向有效数据,然后我创建另一个指针数组(arrcopy),我做——我认为——一个浅拷贝(arrcopy=arr_ptr),我得到了预期的数据……到目前为止还不错 我不明白的是,在我删除arr_ptr后,arrcopy不应该仍然指向我的有效数据吗?为什么没有发生这种情况? 多谢各位 int main() { int a = 1; int b=2; int c=3; int* ap = &a; int* bp = &b;
int main()
{
int a = 1; int b=2; int c=3;
int* ap = &a; int* bp = &b; int* cp = &c;
// Array of pointers
int** arr_ptr = new int*[3];
arr_ptr[0] = ap;
arr_ptr[1] = bp;
arr_ptr[2] = cp;
//shallow copy
int** arrcopy = arr_ptr;
cout << "Values: " << *(arr_ptr[0]) << " " << *(arrcopy[0]) << endl;
cout << "Addresses: " << arr_ptr[0] << " " << arrcopy[0] << endl;
cout << endl;
a++;
cout << "After Incrementing a:" << endl;
cout << *(arr_ptr[0]) << " " << *(arrcopy[0]) << endl;
cout << arr_ptr[0] << " " << arrcopy[0] << endl;
cout << endl;
*(arr_ptr[0]) = 5;
cout << "After updating a value to 5:" << endl;
cout << *(arr_ptr[0]) << " " << *(arrcopy[0]) << endl;
cout << arr_ptr[0] << " " << arrcopy[0] << endl;
cout << endl;
//so far so good - works as expected
//deleting arr_ptr
delete[] arr_ptr;
// Why?: shouldn't arrcopy still be pointing to A
cout << "Values: " << *(arr_ptr[0]) << " " << *(arrcopy[0]) << endl; //The result I am expecting here is: unknown_value 5
cout << "Addresses: " << arr_ptr[0] << " " << arrcopy[0];
cout << endl;
return 0;
}
intmain()
{
INTA=1;INTB=2;INTC=3;
int*ap=&a;int*bp=&b;int*cp=&c;
//指针数组
整数**arr_ptr=新整数*[3];
arr_ptr[0]=ap;
arr_ptr[1]=bp;
arr_ptr[2]=cp;
//浅拷贝
int**arrcopy=arr\u ptr;
cout分配后,您没有2个数组,您有2个指向同一数组的指针:
arr_ptr -------->|ptr0|ptr1|ptr2|ptr3... (allocated memory)
arr_cpy = arr_ptr; // copy only the pointer
现在,两个指针指向相同的分配内存:
这使得:
arr_ptr -------->|xxx0|xxx1|xxx2|xxx3... (invalid memory)
^
arr_cpy -----------|
无论调用哪个指针delete[]
on,它们都指向同一块已分配内存,因此在调用之后,它们都指向同一块已失效内存
您需要做的是复制整个阵列:
int** arr_cpy = new int*[3];
std::copy(arr_ptr, arr_ptr + 3, arr_cpy); // copy the pointers to the new array
或者更好使用std::vector
:
int main()
{
int a = 1; int b=2; int c=3;
int* ap = &a; int* bp = &b; int* cp = &c;
// Array of pointers
std::vector<int*> arr_ptr{3};
arr_ptr[0] = ap;
arr_ptr[1] = bp;
arr_ptr[2] = cp;
//shallow copy
std::vector<int*> arr_cpy = arr_ptr; // copy the whole vector
// ... etc.
intmain()
{
INTA=1;INTB=2;INTC=3;
int*ap=&a;int*bp=&b;int*cp=&c;
//指针数组
std::向量arr_ptr{3};
arr_ptr[0]=ap;
arr_ptr[1]=bp;
arr_ptr[2]=cp;
//浅拷贝
std::vector arr\u cpy=arr\u ptr;//复制整个向量
//……等等。
无需delete[]
,当向量超出范围时,内存将自动释放。无论arr\u ptr
还是arrcopy
都不是数组。两者都只是指针
一个C++指针有点过载。它可以指向一个对象或一个对象数组。
在您的情况下,arr\u ptr
使用动态内存中分配的数组地址进行初始化。但也可以使用堆栈上单个对象的地址进行初始化:
int i = 0;
int** arr_ptr = &i;
通过将该指针的值复制到同一类型的另一个指针中,您只需拥有两个指向同一内存位置的指针:
// array in the heap
[xxxxxxxxxxxxxxxxxxxxxxx]
^
/ \
/ \
/ \
arr_ptr arrcopy
delete[]
操作符还调用数组中所有元素的析构函数。我想你指的是一个简单的删除?arrcopy=arr_ptr
意味着你有两个指向同一数组的指针。通过一个指针删除数组对另一个无效。你没有复制数组,你只复制了指向数组的指针。
// array in the heap
[xxxxxxxxxxxxxxxxxxxxxxx]
^
/ \
/ \
/ \
arr_ptr arrcopy