c++ - 指针数组无法解释的行为(带有浅拷贝)c++

标签 c++ arrays pointers

这是 C++(GNU GCC 编译器)中指针数组的行为,我找不到解释,希望有人能消除困惑。

我正在创建一个指针数组(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* 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;
}

arr_ptr Initialization

将 arr_ptr 浅拷贝到 arrcopy (arrcopy = arr_ptr) 之后 enter image description here

删除arr_ptr后,arrcopy不应该仍然指向a、b、c数据值的内存位置吗?! enter image description here

最佳答案

赋值后,您没有 2 个数组,而是有 2 个指向同一数组的指针:

arr_ptr -------->|ptr0|ptr1|ptr2|ptr3... (allocated memory)


arr_cpy = arr_ptr; // copy only the pointer

现在两个指针都指向相同的分配内存:

arr_ptr -------->|ptr0|ptr1|ptr2|ptr3...
                   ^
arr_cpy -----------|


delete[] arr_ptr; // invalidate the memory that both pointers point to

这给出:

arr_ptr -------->|xxx0|xxx1|xxx2|xxx3... (invalid memory)
                   ^
arr_cpy -----------|

无论您调用哪个指针delete[],它们都指向同一个已分配内存块,因此在调用之后它们都指向同一个无效内存块。 内存。

您需要做的是复制整个数组:

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.

无需delete[],当 vector 超出范围时,内存会自动释放。

关于c++ - 指针数组无法解释的行为(带有浅拷贝)c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41009511/

相关文章:

c++ - 带有 char16_t 或 char32_t 的 Visual Studio C++ 2015 std::codecvt

c++ - 为什么 STL 容器使用复制来填充调整大小?

c++ - 释放容器成员的内存

objective-c - 自动引用计数问题 : Passing address of non-local object to __autoreleasing parameter for write-back

c++ - CUDA 5.0错误LNK2001 : unresolved external symbol for cuda methods

c++ - 结构体中的数组在删除时会被释放吗?

JavaScript数组问题

javascript - 如何访问另一个数组中的对象数组中的 ID 或名称?

C 函数未生成预期的字符串

c++ - 对 Vtable 错误的奇怪 undefined reference