gpt4 book ai didi

c++ - 最终形成指南支持库的无效内存访问

转载 作者:行者123 更新时间:2023-11-30 02:19:12 24 4
gpt4 key购买 nike

我正在试验指南支持库。

这段代码工作得很好,valgrind 没有显示内存问题

#include <gsl>
#include <iostream>

int main() {
const int length = 10;
int *arr = new int [length];
auto _ = gsl::finally([arr] { delete[] arr; });

for (int i = 0; i<length; i++){
arr[i] = i;
}
for (int i = 0; i<length; i++){
std::cout << arr[i] << " ";
}

}

如果删除 finally 语句对 auto _ 的赋值,valgrind 将显示以下无效读/写。为什么我需要将 finally 语句赋给一个值,这个值之后就不再需要了?

我使用 gsl-lite,版本 0.28.0。

损坏的代码:

#include <gsl>
#include <iostream>

int main() {
const int length = 10;
int *arr = new int [length];
gsl::finally([arr] { delete[] arr; });

for (int i = 0; i<length; i++){
arr[i] = i;
}
for (int i = 0; i<length; i++){
std::cout << arr[i] << " ";
}

}

Valgrind 输出:

==15802== Memcheck, a memory error detector
==15802== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==15802== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==15802== Command: ./a.out
==15802==
==15802== Invalid write of size 8
==15802== at 0x400E1A: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e040 is 0 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 8
==15802== at 0x400E24: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e050 is 16 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 4
==15802== at 0x400E2B: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e060 is 32 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid write of size 4
==15802== at 0x400E34: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e064 is 36 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
==15802== Invalid read of size 4
==15802== at 0x400E45: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Address 0x4c3e040 is 0 bytes inside a block of size 40 free'd
==15802== at 0x4A0862D: operator delete[](void*) (vg_replace_malloc.c:621)
==15802== by 0x400E04: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802== Block was alloc'd at
==15802== at 0x4A07898: operator new[](unsigned long) (vg_replace_malloc.c:423)
==15802== by 0x400DF4: main (in /data/home/gwe/projekte/clean_code/gsl/a.out)
==15802==
0 1 2 3 4 5 6 7 8 9 ==15802==
==15802== HEAP SUMMARY:
==15802== in use at exit: 0 bytes in 0 blocks
==15802== total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==15802==
==15802== All heap blocks were freed -- no leaks are possible
==15802==
==15802== For counts of detected and suppressed errors, rerun with: -v
==15802== ERROR SUMMARY: 16 errors from 5 contexts (suppressed: 4 from 4)

最佳答案

这里只是猜测,但似乎当 gsl::finally 返回的对象被破坏时,它调用释放内存的 lambda。

在第一种情况下,这个返回的对象被分配给变量 _,一旦 main 返回,它就会被销毁。在第二种情况下,对象在调用 gsl::finally 后立即被破坏,导致之后对内存的任何访问都是无效的。

关于c++ - 最终形成指南支持库的无效内存访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com