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

时间:2014-02-04 04:22:04

标签: c malloc

我收到错误: * glibc检测到 randtest:free():无效的下一个尺寸(正常):0x00000000023abbe0 * *当两件事之一发生时。首先,当我尝试释放内存时,我有malloced,或者(删除该代码后)我的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)

关于我做错的任何想法?

1 个答案:

答案 0 :(得分:0)

这可能仅仅是“未定义行为”的情况,因为您正在使用size1来初始化array2。一旦你写了超出为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;
}