AddressSanitizer: heap-buffer-overflow on address 0x6020000000b4 at pc 0x0000003a86fc bp 0x7ffeebd5f9d0 sp 0x7ffeebd5f9c8

In

for(int i = 0; i < count.size();i++){
    if(count[i] == (count[i+1])){
        return false;
    }
}

i will reach count.size()-1 and count[i+1] becomes count[count.size()-1+1] which is count[count.size()] and out of range.

A better way to write this loop is

for(int i = 1; i < count.size(); i++){
    if(count[i-1] == (count[i])){
        return false;
    }
}

It starts one later and iterates one less. The exit condition prevents the loop from ever entering if there are less than 2 elements in count making underflowing the buffer impossible.

Note: This answer does not take into account the correctness of the algorithm.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top