gpt4 book ai didi

c++ - "glibc detected - free(): invalid pointer - Program received signal SIGABRT, Aborted"

转载 作者:行者123 更新时间:2023-11-30 00:58:13 26 4
gpt4 key购买 nike

这是让我困惑的最小代码:

#include <list>

class A;

class A {
private:
std::list<A*> a_list;
// int a_list;
public:
A() : a_list() {}
};

int main() {
A* a_tab = new A[1];
// A* a_tab = new A();

delete a_tab;

return 0;
}

在执行时,我得到:

*** glibc detected *** ./main: free(): invalid pointer: 0x0000000000f6d018 ***
======= Backtrace: =========
/lib/libc.so.6(+0x774b6)[0x7f706f2914b6]
/lib/libc.so.6(cfree+0x73)[0x7f706f297c83]
./main[0x4007d2]
/lib/libc.so.6(__libc_start_main+0xfe)[0x7f706f238d8e]
./main[0x400649]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00601000-00602000 r--p 00001000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00602000-00603000 rw-p 00002000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00f6d000-00f8e000 rw-p 00000000 00:00 0 [heap]
7f7068000000-7f7068021000 rw-p 00000000 00:00 0
7f7068021000-7f706c000000 ---p 00000000 00:00 0
7f706f21a000-7f706f394000 r-xp 00000000 08:07 400996 /lib/libc-2.12.1.so
7f706f394000-7f706f593000 ---p 0017a000 08:07 400996 /lib/libc-2.12.1.so
7f706f593000-7f706f597000 r--p 00179000 08:07 400996 /lib/libc-2.12.1.so
7f706f597000-7f706f598000 rw-p 0017d000 08:07 400996 /lib/libc-2.12.1.so
7f706f598000-7f706f59d000 rw-p 00000000 00:00 0
7f706f59d000-7f706f5b2000 r-xp 00000000 08:07 391760 /lib/libgcc_s.so.1
7f706f5b2000-7f706f7b1000 ---p 00015000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b1000-7f706f7b2000 r--p 00014000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b2000-7f706f7b3000 rw-p 00015000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b3000-7f706f835000 r-xp 00000000 08:07 401151 /lib/libm-2.12.1.so
7f706f835000-7f706fa34000 ---p 00082000 08:07 401151 /lib/libm-2.12.1.so
7f706fa34000-7f706fa35000 r--p 00081000 08:07 401151 /lib/libm-2.12.1.so
7f706fa35000-7f706fa36000 rw-p 00082000 08:07 401151 /lib/libm-2.12.1.so
7f706fa36000-7f706fb1e000 r-xp 00000000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fb1e000-7f706fd1d000 ---p 000e8000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd1d000-7f706fd25000 r--p 000e7000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd25000-7f706fd27000 rw-p 000ef000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd27000-7f706fd3c000 rw-p 00000000 00:00 0
7f706fd3c000-7f706fd5c000 r-xp 00000000 08:07 401152 /lib/ld-2.12.1.so
7f706ff3b000-7f706ff40000 rw-p 00000000 00:00 0
7f706ff5a000-7f706ff5c000 rw-p 00000000 00:00 0
7f706ff5c000-7f706ff5d000 r--p 00020000 08:07 401152 /lib/ld-2.12.1.so
7f706ff5d000-7f706ff5e000 rw-p 00021000 08:07 401152 /lib/ld-2.12.1.so
7f706ff5e000-7f706ff5f000 rw-p 00000000 00:00 0
7fffdf34a000-7fffdf36b000 rw-p 00000000 00:00 0 [stack]
7fffdf3da000-7fffdf3db000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abandon

有两行注释:它们都是(独立地)替代它们之前的行的替代方法。

你知道它为什么会这样吗?

我用 g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 编译。

最佳答案

你使用了错误的删除。您使用运算符 new[] 分配了一个只有一个元素的数组,但您使用运算符 delete 作为单个对象删除。请改用 delete[] a_tab;

关于c++ - "glibc detected - free(): invalid pointer - Program received signal SIGABRT, Aborted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843167/

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