一、起源
正如题目所言,最近笔者开始在力扣刷题,由于不熟悉力扣环境,遇到报错也不知道原因是什么,于是开一个帖子记录遇到的报错和可能的原因。
二、报错与可能原因
-
addition of unsigned offset to XXXXXXXX overflowed to XXXXXXXX
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x602000000530 overflowed to 0x60200000052c (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
这个错误是因为访存越界问题,我们仔细观察错误中给出的内存地址,这里我用进制转换器转换到了10进制方便对比,十进制地址差了-4,这里也对应了我的错误产生原因:
访问了nums[-1]
。
-
reference to non-static member function must be called
Line 28: Char 41: error: reference to non-static member function must be called sort(nums1.begin(), nums1.end(), cmp);
提示信息为:
必须调用对非静态成员函数的引用
。
错误产生的原因是我在Solution
类中使用了sort
函数排序并且自定义了cmp
函数,由于自定义的cmp
函数是Solution
的成员函数,所以会包含一个隐含参数this
,而sort
函数只接受两个参数,所以会出错,解决方案是把cmp函数定义成静态函数
,因为静态函数不属于任何一个对象,也不包含隐含参数this
,符合cmp函数的使用规范。 -
待补充
XXXXXX
如果本文能给你带来帮助的话,点个赞鼓励一下作者吧!
三、引用参考
[1] stackoverflow-1:https://stackoverflow.com/questions/66999388/line-1034-char-34-runtime-error-addition-of-unsigned-offset-to-0x6040000000d0