Leetcode C++刷题常见报错

递归没终止

1

=================================================================
==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000084 at pc 0x0000003aa169 bp 0x7ffd45a6d850 sp 0x7ffd45a6d848
READ of size 4 at 0x603000000084 thread T0
    #12 0x7ff02bb060b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x603000000084 is located 0 bytes to the right of 20-byte region [0x603000000070,0x603000000084)
allocated by thread T0 here:
    #9 0x7ff02bb060b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
  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 00 00 fa fa 00 00 00 00 fa fa 00 00
=>0x0c067fff8010:[04]fa fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa
  0x0c067fff8020: 00 00 00 fa fa fa 00 00 00 fa fa fa 00 00 00 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
  0x0c067fff8060: 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
==31==ABORTING

2

AddressSanitizer:DEADLYSIGNAL
=================================================================
==42==ERROR: AddressSanitizer: stack-overflow on address 0x7ffe54906ff8 (pc 0x0000003506e8 bp 0x7ffe54907010 sp 0x7ffe54907000 T0)
==42==ABORTING

链表节点删除有问题

=================================================================
==42==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000000078 at pc 0x0000003e98a0 bp 0x7ffc2a487350 sp 0x7ffc2a487348
READ of size 8 at 0x602000000078 thread T0
    #4 0x7fa78accd0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000078 is located 8 bytes inside of 16-byte region [0x602000000070,0x602000000080)
freed by thread T0 here:
    #4 0x7fa78accd0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
previously allocated by thread T0 here:
    #4 0x7fa78accd0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
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 fd fa fa fa fd fa fa fa 00 00 fa fa fd[fd]
  0x0c047fff8010: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  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
==42==ABORTING

栈没有验空,直接调用top()

Line 170: Char 16: runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment (stl_deque.h)
0xbebebebebebec0ba: note: pointer points here

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_deque.h:180:16

数组越界

Line 1033: Char 34: runtime error: addition of unsigned offset to 0x603000000040 overflowed to 0x60300000003c (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34

新的链表中有指针指向了旧链表

Random pointer of node with label 13 points to a node from the original list.

函数的参数无效(如为NULL)

terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi

原来的代码

    TreeNode* dfs(istringstream &in){
        string str;
        in>>str;
        //if(str=="#") return NULL; // 这里如果没写,就会出现上面的报错。可能一时半会还找不到原因
        TreeNode* root = new TreeNode(stoi(str));
        root->left = dfs(in);
        root->right = dfs(in);
        return root;
    }

参数形式不对

Line 17: Char 11: error: no matching member function for call to 'push'
        q.push(root, 0);
        ~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:259:7: note: candidate function not viable: requires single argument '__x', but 2 arguments were provided
      push(const value_type& __x)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_queue.h:264:7: note: candidate function not viable: requires single argument '__x', but 2 arguments were provided
      push(value_type&& __x)
      ^

如下: q.push({a, b}) 里面是一个pair

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vec;
        queue<pair<TreeNode*, int>> q;
        q.push({root, 0}); // q.push(root,0); 就不对
        while(q.empty()==False){
            TreeNode* tmpNode = q.top()->first;
            int tmplevel = q.top()->second;
            q.pop();
            if(vec.size()==tmplevel) vec.push_back({});
            vec[tmplevel] = tmpNode->val;
            if(tmpNode->left) q.push({tmpNode->left, tmplevel+1})
            if(tmpNode->right) q.push({tmpNode->right, tmplevel+1})
        }
        return vec; 
    }
};

队列

Line 23: Char 27: error: no viable overloaded '='
            vec[tmplevel] = tmpNode->val;
            ~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:692:7: note: candidate function not viable: no known conversion from 'int' to 'const std::vector>' for 1st argument
      operator=(const vector& __x);
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:706:7: note: candidate function not viable: no known conversion from 'int' to 'std::vector>' for 1st argument
      operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:727:7: note: candidate function not viable: no known conversion from 'int' to 'initializer_list>::value_type>' (aka 'initializer_list') for 1st argument
      operator=(initializer_list __l)
      ^
queue.front() 对比 stack.top()
queue<pair<A, B>> q
q.front().first
q.front().second 不是q.front()->second

分母为0

Line 45: Char 41: runtime error: inf is outside the range of representable values of type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:54:41

TLE超时

  • 队列没pop

max的参数类型不一致

上下文

LL res = 0;
int h[N];
res = max(res,h[i] * (h[r[i] - 1] -h[l[i]]) );
// 则 max的两个参数的数据类型分别为 LL 和 int,会导致报错

Line 35: Char 19: error: no matching function for call to ‘max’
res = max(res, h[i] * (h[r[i] - 1] - h[l[i]]));
^~~
/usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/algorithmfwd.h:370:5: note: candidate template ignored: deduced conflicting types for parameter ‘_Tp’ (‘long long’ vs. ‘int’)
max(const _Tp&, const _Tp&);
^
/usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/stl_algo.h:3462:5: note: candidate template ignored: could not match ‘initializer_list’ against ‘long long’
max(initializer_list<_Tp> __l, _Compare __comp)
^
/usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/stl_algo.h:3456:5: note: candidate function template not viable: requires single argument ‘__l’, but 2 arguments were provided
max(initializer_list<_Tp> __l)
^
/usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/algorithmfwd.h:375:5: note: candidate function template not viable: requires 3 arguments, but 2 were provided
max(const _Tp&, const _Tp&, _Compare);
^
1 error generated.

你可能感兴趣的:(算法)