leetcode_1658错误.42==ERROR: AddressSanitizer: heap-buffer-overflow on address

2023-2-14
今天写前缀和,Acwing题目是将数组分成3段使得各自的和相等,求有多少种分法。
出现了段错误 Segmentation Fault

//源代码如下(思路是有问题的)
#include
using namespace std;
int main(void)
{
    long long n,sum=0;
    cin>>n;
    if(n<3) {
        cout<<0;
        return 0;
    }
    long long a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    if(sum%3!=0) cout<<1;
    else cout<<0;
    return 0;
}

参考博客出现的原因有:
https://blog.csdn.net/qq_41115702/article/details/104264018
1、解引用空指针
2、访问不可访问的内存空间(如内核空间)
3、访问不存在的内存地址
4、试图写一个只读内存空间(如代码段)
5、栈溢出(函数递归调用太深)
6、使用未初始化的指针(定义时没有初始化或者已经回收)

leetcode_1658错误.42==ERROR: AddressSanitizer: heap-buffer-overflow on address_第1张图片
leetcode_1658错误.42==ERROR: AddressSanitizer: heap-buffer-overflow on address_第2张图片

自己写的源代码


int minOperations(int* nums, int numsSize, int x){
    int left=-1,right=numsSize;
    int lsum=0,rsum=0;
    for(int i=0;i<numsSize;i++){
        rsum+=nums[i];
    }
    if(rsum<x) return -1;
    while(left<right&&right<=numsSize){
        if(lsum+rsum==x){
            return (left+1+numsSize-right);
        }else if(lsum+rsum<x){
             left++;
            lsum+=nums[left];
         }else{
            right++;
            rsum-=nums[right];
        }
    }
    return -1;
}

Leetcode控制台显示错误

=================================================================
==42==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000028 at pc 0x563e29fb83e9 bp 0x7fff940ff6c0 sp 0x7fff940ff6b0
READ of size 4 at 0x603000000028 thread T0
    #2 0x7f138ee34082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
0x603000000028 is located 4 bytes to the right of 20-byte region [0x603000000010,0x603000000024)
allocated by thread T0 here:
    #0 0x7f138fa7c808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #3 0x7f138ee34082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
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 04[fa]fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8020: fa fa fa 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
==42==ABORTING

后来发现是数组越界。

你可能感兴趣的:(算法刷题,leetcode,算法)