C语言用calloc分配内存,有时会报错,有时能编译通过,我还啥也没干呢就报错,同样的代码第一次点能通过,再点就报错了,之后再点偶尔还能通过,求大神帮忙看看
int sumnum(int numsSize) //计算全排列的个数
{
int ret =1;
for (int i = 1; i <= numsSize; i++)
{
ret = ret * i;
}
return ret;
}
int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
{
int sum = 0;
sum = sumnum(numsSize);
int **a = (int **)calloc(sum, sizeof(int *));
int *book = (int *)calloc(numsSize, sizeof(int));
for(int i = 0;i<sum;i++)
{
a[i] = (int*)calloc(numsSize,sizeof(int));
}
return a;
}
报错信息:=================================================================
==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000028 at pc 0x00000040500d bp 0x7ffd53c29910 sp 0x7ffd53c29900
READ of size 4 at 0x603000000028 thread T0
#3 0x7fcd58c1d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x603000000028 is located 0 bytes to the right of 24-byte region [0x603000000010,0x603000000028)
allocated by thread T0 here:
#0 0x7fcd59c3917e in calloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c17e)
#3 0x7fcd58c1d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c067fff8000: fa fa 00 00 00[fa]fa fa 00 00 00 fa fa fa 00 00
0x0c067fff8010: 00 fa fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa
0x0c067fff8020: 00 00 00 fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==29==ABORTING