leetcode报错:==45==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x000

本文深入解析了一道力扣题目的解答过程中遇到的堆缓冲区溢出错误,通过具体代码示例展示了如何定位并解决该问题。同时,分享了解决面试题57-II关于和为s的连续正数序列的双指针算法实现。

今天的力扣一题,题目还行,就死在了下列这个报错中。还是分享一下题解吧,在后方。

力扣报错:

=================================================================
==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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值