简体   繁体   中英

I'm solving a leetcode question and getting this error. I have no idea what this mean as I'm relatively new to C++

I'm not able to run the code for a while now what can I do to sort this error out.

AddressSanitizer: DEADLYSIGNAL ================================================================= ==32==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000383e8c bp 0x7ffc55bebe50 sp 0x7ffc55bebd20 T0) ==32==The signal is caused by a READ memory access. ==32==Hint: address points to the zero page. #3 0x7f2222e3982f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) AddressSanitizer can not provide additional info. ==32==ABORTING

class Solution {
    public:
        int firstMissingPositive(vector<int>& nums) {
            int n=nums.size();
            vector<int>ans(50);
            for(int i=0; i<n; i++){
                if(nums[i]<0) continue;
                ans[nums[i]]++;
            }
            for(int i=1; i<n; i++){
                if(ans[i]==0){
                    return i;
                }
            }
            return n+1;
        }
    };

You have an out-of-bounds error. When you write something like this, make sure at least it has the same number or more elements than your nums array.

vector<int>ans(50);

it goes out of the boundary in this loop.

for(int i=0; i<n; i++)
{
   if(nums[i]<0) continue;
   ans[nums[i]]++;

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM