Reputation: 31
I am solving this problem https://codeforces.com/problemset/problem/158/A and I am getting no error with address sanitizer but when the input is n = 1, k = 1 address sanitizer gives error of "heap-buffer-overflow".
My code
int main(int argc, char const *argv[]) {
int n, k;
cin >> n >> k;
int count = 0;
vector<int> v;
while (n) {
--n;
int a;
cin >> a;
v.push_back(a);
}
int i = 0;
while (i < v.size()) {
if (v[i] >= v[k] && v[i] > 0) {
++count;
}
++i;
}
cout << count << "\n";
return 0;
}
Upvotes: 0
Views: 126
Reputation: 21954
Actually Address sanitizer does work and helpfully detects a bug in your code: you are allocating vector of n
elements and then access v[k]
. In your case you are trying to access second element (v[1]
) of 1-sized vector which causes buffer overflow.
Upvotes: 0