gpt4 book ai didi

c - "free(): invalid pointer"错误和核心转储

转载 作者:行者123 更新时间:2023-11-30 15:48:56 25 4
gpt4 key购买 nike

在成功执行程序并释放动态分配的动态内存时,我得到了这个不需要的输出。

*** glibc detected *** /home/ahor/Desktop/Project Work/node: free(): invalid pointer: 0xb7fda000 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e93ee2]
/home/ahor/Desktop/Project Work/node[0x8048cf9]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e374d3]
/home/ahor/Desktop/Project Work/node[0x8048561]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:06 786473 /home/ahor/Desktop/Project Work/node
0804a000-0804b000 r--p 00001000 08:06 786473 /home/ahor/Desktop/Project Work/node
0804b000-0804c000 rw-p 00002000 08:06 786473 /home/ahor/Desktop/Project Work/node
0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
b7deb000-b7e07000 r-xp 00000000 08:06 2228253 /lib/i386-linux-gnu/libgcc_s.so.1
b7e07000-b7e08000 r--p 0001b000 08:06 2228253 /lib/i386-linux-gnu/libgcc_s.so.1
b7e08000-b7e09000 rw-p 0001c000 08:06 2228253 /lib/i386-linux-gnu/libgcc_s.so.1
b7e1d000-b7e1e000 rw-p 00000000 00:00 0
b7e1e000-b7fc1000 r-xp 00000000 08:06 2228351 /lib/i386-linux-gnu/libc-2.15.so
b7fc1000-b7fc3000 r--p 001a3000 08:06 2228351 /lib/i386-linux-gnu/libc-2.15.so
b7fc3000-b7fc4000 rw-p 001a5000 08:06 2228351 /lib/i386-linux-gnu/libc-2.15.so
b7fc4000-b7fc7000 rw-p 00000000 00:00 0
b7fd7000-b7fd8000 rw-p 00000000 00:00 0
b7fd8000-b7fd9000 rw-s 00000000 00:04 12517386 /SYSV001120bd (deleted)
b7fd9000-b7fda000 rw-p 00000000 00:00 0
b7fda000-b7fdb000 rw-s 00000000 00:04 12484617 /SYSV00307eff (deleted)
b7fdb000-b7fdd000 rw-p 00000000 00:00 0
b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
b7fde000-b7ffe000 r-xp 00000000 08:06 2228363 /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 08:06 2228363 /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 08:06 2228363 /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]

......
......
......

当我使用 gdb 回溯时收到此消息。

Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fdd424 in __kernel_vsyscall ()
#1 0xb7e4c1df in raise () from /lib/i386-linux-gnu/libc.so.6
#2 0xb7e4f825 in abort () from /lib/i386-linux-gnu/libc.so.6
#3 0xb7e8939a in ?? () from /lib/i386-linux-gnu/libc.so.6
#4 0xb7e93ee2 in ?? () from /lib/i386-linux-gnu/libc.so.6
#5 0x08048cf9 in main (argc=6, argv=0xbffff214) at node.c:180

............这是我的代码段,它会影响这个?我无法查出原因,如果您能帮助我,我将非常感激。

// creating the dynamic memory for net
if((net = (struct dot **)malloc(v * sizeof(struct dot))) == NULL) {
perror("struct dot malloc() failed\n");
exit(1);
}
for(i = 0 ; i < v; i++)
if((net[i] = (struct dot *)malloc(v * sizeof(struct dot))) == NULL) {
perror("net[i] malloc() failed.\n");
for(j = 0; j < i; j++)
free(net[j]);
free(net);
}
.....
.....
// creating the dynamic memory for link_status
if((link_status = (struct link **)malloc(v * sizeof(struct link))) == NULL) {
perror("struct link malloc() failed\n");
exit(1);
}
for(i = 0 ; i < v; i++)
if((link_status[i] = (struct link *)malloc(v * sizeof(struct link))) == NULL) {
perror("link_status[i] malloc() failed.\n");
for(j = 0; j < i; j++)
free(link_status[j]);
free(link_status);
}
......

检测到的 glibc 来自下面使用的 free() 函数。

// free the allocated dynamic memories
for(i = 0; i < v; i++) {
free(net[i]);
free(link_status[i]);
}
free(net);
free(link_status);

请帮助我!

最佳答案

您正在覆盖一些不属于您的内存。您可以使用 valgrind 查找导致错误写入的代码。

关于c - "free(): invalid pointer"错误和核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16543820/

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