mardi 16 février 2021

C++ runtime error: addition of unsigned offset

In C++ my code was working perfect until I added:

if (n-k<4) return;

in:

void rotate(vector<int>& nums, int k) {
        int n=(int) nums.size();
        for (int i=n-1;i>=n-k;--i)
        {
            int tmp=nums[i];
            nums[i]=nums[i-k];
            nums[i-k]=tmp;
        }
        if (n-k<4) return;
    }

error message:

Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6020000002b0 overflowed to 0x6020000002ac (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34

what is causing this problem?

Aucun commentaire:

Enregistrer un commentaire