尝试在 C 中动态调整数组大小时崩溃?

发布于 2025-01-02 21:43:12 字数 3091 浏览 0 评论 0原文

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

#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)

你能用新手术语解释一下吗?

Right now, I want to increase the size of the array using a function.

#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;
}

I ran the code with:
valgrind --leak-check=full ./test 2>debug.txt

and this is what I got for the output:

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!

and this is what I got at the 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)

Could you explain this in newbie terms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2025-01-09 21:43:12

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

void IncreaseArraySize(int*& addr){

现在,您的错误是由于当您

IncreaseArraySize(test);

main 中调用 test 指针 时引起的没有被重新分配。因此,一旦您在 IncreaseArraySizedelete[]它,它就会引用垃圾内存。更新参数以使其通过引用传递意味着当您在 IncreaseArraySize 中说

addr = temp;

这将更新 main 中的 test 指针,从而防止漏洞。

希望这有帮助!

I believe that your problem is that because you are passing the pointer to the start of the array by value, once you've updated and reassigned it, the changes aren't propagating to the caller. If you change the function so that it takes the pointer by reference, this should be fixed:

void IncreaseArraySize(int*& addr){

Right now, your bug is caused because when you call

IncreaseArraySize(test);

The test pointer back in main isn't getting reassigned. As a result, once you delete[] it in IncreaseArraySize, it references garbage memory. Updating the parameter so that it's passed by reference means that when, in IncreaseArraySize, you say

addr = temp;

This will update the test pointer in main, preventing the bug.

Hope this helps!

携君以终年 2025-01-09 21:43:12

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

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

void IncreaseArraySize(int*& addr)

Well the most appropriate way to correct this code is to not use new at all, just use:
std:vector

Also, the problem with your code in particular is that you are passing the pointer addr by value, which creates a temporary and passes it to the function. Any changes made to this pointer inside the function are made on the copy of the pointer and not the original pointer.You need addr by reference, so that the changes inside the function are made on the pointer and get reflected outside the function.

void IncreaseArraySize(int*& addr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文