zoukankan      html  css  js  c++  java
  • 程序编译过程中错误记录

    1 越界(做leecode题时显示的错误)

    程序如下

    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
        int* twoSum(int* nums, int numsSize, int target, int* returnSize){
        int j = 0;      //用来搜索第一个数
        int k = 0;      //用来搜索第二个数
        int *a;
        int flag = 0;
        a = (int *)malloc(sizeof(int) * 2);
        *returnSize = 0;
        for(j = 0; j < numsSize; j++)
        {
            for(k = j + 1; k <= numsSize; k++)
            {
               if(target == nums[j] + nums[k])   //判断两值之和是否为目标值
                {
                    *a = j;
                    *(a + 1) = k;
                    *returnSize = 2;
                    flag = 1;
                 }
            }
            if(flag == 1)   break;
        }
        return a;
    }

    编译后结果如下,显示越界

    =================================================================
    ==46==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000020 at pc 0x0000004019cc bp 0x7ffc46336880 sp 0x7ffc46336870
    READ of size 4 at 0x602000000020 thread T0
        #2 0x7f4d0107a82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    0x602000000020 is located 0 bytes to the right of 16-byte region [0x602000000010,0x602000000020)
    allocated by thread T0 here:
        #0 0x7f4d02095f88 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10bf88)
        #3 0x7f4d0107a82f 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 00[fa]fa 00 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
    ==46==ABORTING

    错误分析:

    比如一个数组a[4] = {2,7,11,15};

    那么numSize就等于4,而在for循环中,J和K都是下标,

    for(j = 0; j < numsSize; j++)//表明J可以取值3
    for(k = j + 1; k <= numsSize; k++) //当J为3时,K就为4,而a[4]这个数就越界了。

    修改过后通过

    2 left operand must be l-value

    不能给常量赋值

    void main()
    {
        int a[4] = {2,7,11,15};
        int b[2] ={0,0};
        b = twoSum(a, 4, 26, b);//只能给变量赋值
        printf("%d,%d
    ",b[0],b[1]);
    }

    3 undefined reference to `WinMain@16'

    解决方法

    4 member access within null pointer of type 'struct TreeNode'

    翻译:访问一个空节点的成员

    解决方法:在访问之前判断一下该节点是否为空。

    参考:leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    链接:https://www.cnblogs.com/qq952693358/p/9280717.html

    5 if语句中的判断条件在改变

    if(a[i] > temp)
        temp += a[i];

    以上两条语句,本意是想把数组中大于temp的数,就进行累加,但累加后,temp就改变了,判断条件也就改变了。

  • 相关阅读:
    Ftp、Ftps与Sftp之间的区别
    Previous Workflow Versions in Nintex Workflow
    Span<T>
    .NET Core 2.0及.NET Standard 2.0 Description
    Announcing Windows Template Studio in UWP
    安装.Net Standard 2.0, Impressive
    SQL 给视图赋权限
    Visual Studio for Mac中的ASP.NET Core
    How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1
    用于Azure功能的Visual Studio 2017工具
  • 原文地址:https://www.cnblogs.com/QQ2962269558/p/12770676.html
Copyright © 2011-2022 走看看