-1

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;
        }
    };
Avinash
  • 1
  • 1
  • Give a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Jason Aug 26 '22 at 10:28
  • 2
    This is why c++ must be learnt using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of by solving random online puzzles. – Jason Aug 26 '22 at 10:29
  • 3
    If you're "relatively new to C++" and still learning, then step away from that site. No so-called "competition" or "judge" site is a learning or teaching resource, that's just not their purpose no matter what you might have been told. If you can then invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take computer-science classes. If it's not possible there's plenty of online tutorials (but stay away from Youtube, there's too many low-quality videos there). – Some programmer dude Aug 26 '22 at 10:31
  • 1
    As a hint about your current problem: Think about what happens if `n >= 50`. – Some programmer dude Aug 26 '22 at 10:32
  • By the way, `for(int i=1; i – Some programmer dude Aug 26 '22 at 12:11

1 Answers1

0

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]]++;

 }
persona
  • 70
  • 7
  • 1
    In that loop there *might* be an out-of-bounds condition, but we can't tell since we don't know what the values in `nums` might be. In the *other* loop... – Some programmer dude Aug 26 '22 at 11:58
  • I assumed it tries to change the value out of the boundary condition. The error hints at it as a read memory address. Yeah, you're right we don't know the values in nums. – persona Aug 26 '22 at 12:07