【发布时间】:2022-01-07 03:14:33
【问题描述】:
我在解决 leetcode 上的 this question 时遇到此运行时错误。
第 1034 行:字符 9:运行时错误:引用绑定到“int”类型的空指针 (stl_vector.h) 摘要:UndefinedBehaviorSanitizer:未定义行为 /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h :1043:9
这是我的代码-
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int m=nums1.size();
int n=nums2.size();
vector<int> nums3;
int i=0,j=0,k=0;
while(i<m && j<n)
{
if(nums1[i]<=nums2[j])
nums3[k++]=nums1[i++];
else
nums3[k++]=nums2[j++];
}
while(i<m)
nums3[k++]=nums1[i++];
while(j<n)
nums3[k++]=nums2[j++];
if(k%2==0)
return ((nums3[k/2] + nums3[(k/2)-1])/2);
else
return nums3[k/2];
}
};
【问题讨论】:
-
在中断点,使用调试器获取堆栈跟踪。这将返回您的代码,然后您可以看到您的代码正在执行的哪些操作触发了错误。
-
有趣的事实:当我搜索错误消息 (
[c++] "reference binding to null pointer of type") 时,似乎所有的点击都是来自试图解决 Leetcode 问题的人的问题。奇怪的巧合,还是更系统的关于 Leetcode 作为资源的质量?
标签: c++ sorting runtime-error