【问题标题】:Crash when trying to dynamically resize an array in C++?尝试在 C++ 中动态调整数组大小时崩溃?
【发布时间】:2015-08-20 16:32:06
【问题描述】:

现在,我想使用一个函数来增加数组的大小。

#include <iostream>
using namespace std;

void IncreaseArraySize(int* addr){
    int* temp = new int[20];
    for(int i=0;i<10;i++){
        temp[i] = addr[i];
    }
    for(int i=10;i<20;i++){
        temp[i] = i;
    }
    int* dummy = addr;
    addr = temp;
    delete[] dummy;
}

int main(){
    int* test = new int[10];
    for(int i=0;i<10;i++){
        test[i] = i;
    }
    IncreaseArraySize(test);
    for(int i=0;i<20;i++){
        cout<<"at index "<<i<<"we have"<<test[i]<<endl;
    }
    cout<<"ok!"<<endl;
    delete[] test;
}

我使用以下代码运行代码: valgrind --leak-check=full ./test 2>debug.txt

这就是我得到的输出:

at index 0we have0
at index 1we have1
at index 2we have2
at index 3we have3
at index 4we have4
at index 5we have5
at index 6we have6
at index 7we have7
at index 8we have8
at index 9we have9
at index 10we have0
at index 11we have0
at index 12we have0
at index 13we have0
at index 14we have0
at index 15we have0
at index 16we have0
at index 17we have0
at index 18we have112
at index 19we have0
ok!

这就是我在 debug.txt 中得到的:

==4285== Memcheck, a memory error detector
==4285== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4285== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==4285== Command: ./test
==4285== 
==4285== Invalid read of size 4
==4285==    at 0x400997: main (test.cpp:24)
==4285==  Address 0x596f040 is 0 bytes inside a block of size 40 free'd
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400931: IncreaseArraySize(int*) (test.cpp:14)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== Invalid free() / delete / delete[]
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400A16: main (test.cpp:27)
==4285==  Address 0x596f040 is 0 bytes inside a block of size 40 free'd
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400931: IncreaseArraySize(int*) (test.cpp:14)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== 
==4285== HEAP SUMMARY:
==4285==     in use at exit: 80 bytes in 1 blocks
==4285==   total heap usage: 2 allocs, 2 frees, 120 bytes allocated
==4285== 
==4285== 80 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4285==    at 0x4C2864B: operator new[](unsigned long) (vg_replace_malloc.c:305)
==4285==    by 0x4008A9: IncreaseArraySize(int*) (test.cpp:5)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== LEAK SUMMARY:
==4285==    definitely lost: 80 bytes in 1 blocks
==4285==    indirectly lost: 0 bytes in 0 blocks
==4285==      possibly lost: 0 bytes in 0 blocks
==4285==    still reachable: 0 bytes in 0 blocks
==4285==         suppressed: 0 bytes in 0 blocks
==4285== 
==4285== For counts of detected and suppressed errors, rerun with: -v
==4285== ERROR SUMMARY: 22 errors from 3 contexts (suppressed: 4 from 4)

你能用新手的话解释一下吗?

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/q/9167540/535275
  • addr = temp; 并没有按照你的想法去做。
  • 我认为 addr=temp 应该将 temp 保存的内容(因为 temp 是一个指针,指向某个堆空间的地址)复制到 addr...猜 c++ 对我来说太混乱了

标签: c++ arrays new-operator delete-operator


【解决方案1】:

我相信你的问题是因为你通过值传递指向数组开头的指针,一旦你更新并重新分配它,更改不会传播给调用者。如果您更改函数以通过引用获取指针,则应修复此问题:

void IncreaseArraySize(int*& addr){

现在,你的错误是因为当你调用时引起的

IncreaseArraySize(test);

main 中的 test 指针未重新分配。结果,一旦你在IncreaseArraySizedelete[] 它,它就会引用垃圾内存。更新参数以使其通过引用传递意味着当您在IncreaseArraySize 中说

addr = temp;

这将更新main 中的test 指针,防止出现错误。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    纠正此代码最合适的方法是根本不使用new,只需使用:
    std:vector

    此外,您的代码的问题尤其是您正在按值传递指针addr,这会创建一个临时变量并将其传递给函数。在函数内部对这个指针所做的任何更改都是在指针的副本上而不是在原始指针上进行的。您需要通过引用addr,以便函数内部的更改在指针上进行并反映在函数外部。

    void IncreaseArraySize(int*& addr)
    

    【讨论】:

    • @user269334:实际上,也可以考虑使用第一个建议,这更合适,当然,即使您将std::vector 传递给函数,&amp; 也很重要。Do Lookup Pass在 C++ 中按值与按引用传递
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2012-10-02
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多