Trying to solve Leetcode problem #448: Find All Numbers Disappeared in an Array. The Variable nums represents a vector containing a list of numbers (including the disappeared ones).
vector <int> temp,result;
int max = *max_element(nums.begin(),nums.end());
for(int i=1;i<=max;i++)
temp.push_back(i);
for(int i=0;i<int(temp.size());i++)
{
if(count(nums.begin(),nums.end(),temp[i])==0)
{
result.push_back(temp[i]);
}
}
return result;
There seems to be no compile time error. But Runtime throws an error saying:
Line 811: Char 16: runtime error: reference binding to null pointer of type 'int' (stl_iterator.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_iterator.h:820:16
numshas data in it? If not, then*max_element(nums.begin(),nums.end())would certainly be a likely candidate for the error as you would be dereferencing a "null iterator".