英文:
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<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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论