gpt4 book ai didi

c - 检测到 glibc - 主方法返回时崩溃

转载 作者:行者123 更新时间:2023-11-30 17:40:21 25 4
gpt4 key购买 nike

我收到错误:* glibc 检测到 randtest: free(): 无效的下一个大小(正常): 0x00000000023abbe0 **当两件事之一发生时。首先,当我尝试释放已分配的内存时,或者(删除该代码后)我的 main 方法返回(这也会隐式释放该内存)。

这是代码(没有免费)。我正在尝试构建两个随机大小的排序数组来测试函数。

int main(int argc, char **argv) {
int tests = 10;
int i;
int j;
int *array1;
int *array2;
int holder;
int size1;
int size2;
for(i = 0; i < tests; i++){
size1 = rand() % 30;
array1 = malloc(sizeof(int) * (size1));
size2 = rand() % 30;
array2 = malloc(sizeof(int) * (size2));
for (j = 0; j < size1; j++){
if (j == 0)
holder = 0;
else
holder = array1[j-1];
array1[j] = rand() %5 + holder;
}
for (j = 0; j < size1; j++){
if (j == 0)
holder = 0;
else
holder = array2[j-1];
array2[j] = rand() %5 + holder;
}
merge(array1, size1, array2, size2);
//assert(isSorted(merge(array1, size1, array2, size2), size1+size2) == 0);
}
return 0;
}

完整的错误跟踪:

 ======= Backtrace: =========
/lib64/libc.so.6[0x3751876166]
/lib64/libc.so.6[0x3751878c93]
/lib64/libc.so.6(fclose+0x14d)[0x37518667cd]
randtest[0x4019df]
randtest[0x402306]
/lib64/libc.so.6(exit+0xe2)[0x3751835e22]
/lib64/libc.so.6(__libc_start_main+0x104)[0x375181ed24]
randtest[0x400d09]
======= Memory map: ========
00400000-00404000 r-xp 00000000 00:22 33295023 /nfs/stak/students/t/todtm/src/midterm/randtest
00603000-00604000 rw-p 00003000 00:22 33295023 /nfs/stak/students/t/todtm/src/midterm/randtest
00604000-00605000 rw-p 00000000 00:00 0
023ab000-023cc000 rw-p 00000000 00:00 0 [heap]
3751000000-3751020000 r-xp 00000000 fd:00 131107 /lib64/ld- 2.12.so
375121f000-3751220000 r--p 0001f000 fd:00 131107 /lib64/ld-2.12.so
3751220000-3751221000 rw-p 00020000 fd:00 131107 /lib64/ld-2.12.so
3751221000-3751222000 rw-p 00000000 00:00 0
3751800000-375198b000 r-xp 00000000 fd:00 131112 /lib64/libc-2.12.so
375198b000-3751b8a000 ---p 0018b000 fd:00 131112 /lib64/libc-2.12.so
3751b8a000-3751b8e000 r--p 0018a000 fd:00 131112 /lib64/libc-2.12.so
3751b8e000-3751b8f000 rw-p 0018e000 fd:00 131112 /lib64/libc-2.12.so
3751b8f000-3751b94000 rw-p 00000000 00:00 0
3751c00000-3751c83000 r-xp 00000000 fd:00 131151 /lib64/libm-2.12.so
3751c83000-3751e82000 ---p 00083000 fd:00 131151 /lib64/libm-2.12.so
3751e82000-3751e83000 r--p 00082000 fd:00 131151 /lib64/libm-2.12.so
3751e83000-3751e84000 rw-p 00083000 fd:00 131151 /lib64/libm-2.12.so
3756400000-3756416000 r-xp 00000000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
3756416000-3756615000 ---p 00016000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
3756615000-3756616000 rw-p 00015000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
7fd4fae39000-7fd4fae3c000 rw-p 00000000 00:00 0
7fd4fae65000-7fd4fae68000 rw-p 00000000 00:00 0
7fffbdf80000-7fffbdf95000 rw-p 00000000 00:00 0 [stack]
7fffbdfff000-7fffbe000000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abort (core dumped)

关于我做错了什么有什么想法吗?

最佳答案

这可能只是“未定义行为”的情况,因为您在初始化 array2 的循环中使用了 size1。一旦您写入的内容超出了为 array2 预留的内存,您就会覆盖 malloc 用于跟踪内存分配的一些额外数据,此时所有的赌注都将被取消。

此外,如果您想要随机数,您应该调用 srand 来为 RNG 提供种子。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv) {
int tests = 10;
int i, j;
int *array1, *array2;
int size1, size2;

srand((unsigned)time(NULL));

for(i = 0; i < tests; i++){

size1 = rand() % 30;
array1 = malloc(sizeof(int) * size1);

size2 = rand() % 30;
array2 = malloc(sizeof(int) * size2);

array1[0] = rand() % 5;
for (j = 1; j < size1; j++)
array1[j] = rand() % 5 + array1[j-1];

array2[0] = rand() % 5;
for (j = 1; j < size2; j++)
array2[j] = rand() % 5 + array2[j-1];

merge(array1, size1, array2, size2);

//assert(isSorted(merge(array1, size1, array2, size2), size1+size2) == 0);
}

return 0;
}

关于c - 检测到 glibc - 主方法返回时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542901/

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