为什么会发生“AddressSanitizer: heap-buffer-overflow”错误?

huangapple go评论79阅读模式
英文:

Why this error of "AddressSanitizer: heap-buffer-overflow" is happening?

问题

I am a beginner. I am getting an error of

==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000118 at pc 0x000000345e5c bp 0x7ffe75dfbff0 sp 0x7ffe75dfbfe8
READ of size 4 at 0x602000000118 thread T0

The error message is long so the above is just a snippet of it. I think the error is due to the line which I highlighted here with comments but I am not sure

class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr)
    {

        vector<int> temp(10, 0);
        int n = arr.size(), i = 0, k = 0, j = 0;

        while (i < n) {
            for (int j = 0; j < n; j++) {
                if (arr[j] == arr[i]) {
                    temp[k]++;
                    arr[j] = -1000;
                }
            }

            while (arr[i] == -1000 && i < 1000) // THIS LINE
                i++;
            k++;
        }

        for (i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++) {
                if (temp[i] == temp[j])
                    return false;
            }

        return true;
    }
};

I was making a simple program to find a unique number in an array

英文:

I am a beginner. I am getting an error of

==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000118 at pc 0x000000345e5c bp 0x7ffe75dfbff0 sp 0x7ffe75dfbfe8
READ of size 4 at 0x602000000118 thread T0

The error message is long so the above is just a snippet of it. I think the error is due to the line which I highlighted here with comments but I am not sure

class Solution {
public:
    bool uniqueOccurrences(vector&lt;int&gt;&amp; arr)
    {

        vector&lt;int&gt; temp(10, 0);
        int n = arr.size(), i = 0, k = 0, j = 0;

        while (i &lt; n) {
            for (int j = 0; j &lt; n; j++) {
                if (arr[j] == arr[i]) {
                    temp[k]++;
                    arr[j] = -1000;
                }
            }

            while (arr[i] == -1000 &amp;&amp; i &lt; 1000) // THIS LINE
                i++;
            k++;
        }

        for (i = 0; i &lt; n; i++)
            for (int j = i + 1; j &lt; n; j++) {
                if (temp[i] == temp[j])
                    return false;
            }

        return true;
    }
};

I was making a simple program to find a unique number in an array

答案1

得分: 3

while(arr[i]==-1000) // 这一行
i++;

There's no limit on i, it can go out of bounds as long as the condition you did write is true.

英文:
while(arr[i]==-1000)   // THIS LINE
    i++;

There's no limit on i, it can go out of bounds as long as the condition you did write is true.

huangapple
  • 本文由 发表于 2023年4月13日 22:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定