今天的力扣一题,题目还行,就死在了下列这个报错中。还是分享一下题解吧,在后方。
力扣报错:
=================================================================
==45==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x000000401b4d bp 0x7ffd417069e0 sp 0x7ffd417069d0
WRITE of size 4 at 0x602000000018 thread T0
#2 0x7fc31eb5982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x602000000018 is located 0 bytes to the right of 8-byte region [0x602000000010,0x602000000018)
allocated by thread T0 here:
#0 0x7fc31fb75078 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c078)
#3 0x7fc31eb5982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 00[fa]fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: 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
==45==ABORTING
解决方案:
遇到过好几次这种情况,每次都是最终发现自己的数组大小定义小了,导致访问越界。
面试题57 - II. 和为s的连续正数序列

解题思路就是官方解题的 双指针
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** findContinuousSequence(int target, int* returnSize, int** returnColumnSizes){
if(target <= 0 ){
return NULL;
}
int sum,l,r;
int j;
int** res = (int**)malloc(sizeof(int*)*target);
*returnColumnSizes = (int*)malloc(sizeof(int)*target);
*returnSize = 0;
for(l = 1,r = 2;l < r;)
{
sum = (l + r)*(r - l+1)/2;
if(sum == target) {
j=0;
res[*returnSize] = (int*)malloc(sizeof(int) * (r - l+1)); //死在这里数组大小分配少了1个
(*returnColumnSizes)[*returnSize] = r - l+1; //还有这里数组大小分配少了1个
for (int k = l; k <= r; k++) {
res[*returnSize][j++] = k;
}
(*returnSize)++;
l++;
}
else if(sum < target) r++; //窗口变大
else l++;
}
return res;
}
本文深入解析了一道力扣题目的解答过程中遇到的堆缓冲区溢出错误,通过具体代码示例展示了如何定位并解决该问题。同时,分享了解决面试题57-II关于和为s的连续正数序列的双指针算法实现。

704

被折叠的 条评论
为什么被折叠?



