-YIN 2021-10-06 22:38 采纳率: 100%
浏览 56
已结题

力扣53:最大子序和(最大子段和C语言分治)

小白求指点,为什么会报错

执行结果: 执行出错 ================================================================= ==42==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000034 at pc 0x5643b9ff8e04 bp 0x7ffd2cd0ab60 sp 0x7ffd2cd0ab50 READ of size 4 at 0x604000000034 thread T0 #5 0x7f15c13530b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 0x604000000034 is located 0 bytes to the right of 36-byte region [0x604000000010,0x604000000034) allocated by thread T0 here: #0 0x7f15c1f98bc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8) #3 0x7f15c13530b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) Shadow bytes around the buggy address: 0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c087fff8000: fa fa 00 00 00 00[04]fa fa fa fa fa fa fa fa fa 0x0c087fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8050: 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

代码


int maxSubArray(int nums[], int numsSize){
    return (maxSubSum(nums, 0, numsSize));
}

int maxSubSum(int *nums, int left, int right){
    int  sum = 0, left_sum = 0, right_sum = 0, allmax = 0;
    if (left == right){
        if (nums[left] > 0){
            return nums[left];
        }
        else{
            return 0;
        }
    }

    int mid = (left + right) / 2;
    left_sum = maxSubSum(nums, left, mid);
    right_sum = maxSubSum(nums, mid + 1, right);
    for (int i = mid; i >= left; i--){
        sum += nums[i];
        if (allmax < sum){
            allmax = sum;
        }
    }
    sum = allmax;
    for (int i = mid + 1; i <= right; i++){
        sum += nums[i];
        if (allmax < sum){
            allmax = sum;
        }
    }
    if (allmax < left_sum && right_sum < left_sum){
        return left_sum;
    }
    if (allmax < right_sum){
        return right_sum;
    }
    return allmax;
}

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-06 22:43
    关注

    return (maxSubSum(nums, 0, numsSize-1)试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月6日

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化