Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to solve a Pascal's Triangle problem on Leetcode. I get this error when I run the code.
AddressSanitizer: heap-buffer-overflow on address 0x602000000014 at pc 0x000000407875 bp 0x7ffe13bd9300 WRITE of size 4 at 0x602000000014 thread T0.
How should I fix it?
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> tri(numRows);
vector<int> row;
row.push_back(1);
tri.push_back(row);
row.clear();
for (int i = 1; i < numRows; i++) {
row[0]=1;
row[i]=1;
for (int j = 1; j < i; j++) {
row[j] = tri[i-1][j] + tri[i-1][j-1];
}
tri[i] = row;
row.clear();
}
return tri;
}
};
When you call row.clear(), that wipes the row and sets the length to 0. As a result, when you try accessing row[0], row[i], etc, you're accessing memory that you shouldn't be touching.
Here's your problem:
row[0]=1;
row[i]=1;
At this point, the row vector is empty. So you are writing into thin air.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i got a 5x5 matrix and i need to hash its values.
so i'm trying to hash the 2nd lines and 3rd columns like this
void hashing(int matris[5][5]) {
int x = 0;
int y = 0;
cin >> x;
cin >> y;
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
if ((i==x-1)|| (k==y-1))
{
matris[i][k] = "*";
}
}
}
}
thats the code but i got a error that follows:
"a value of type "const char*" can not be assigned to an entity of type "int""
does anyone knows how to do it ?
When you declare int matrix[5][5] you are telling your program that you want to store integers in that matrix, basically numbers. So the error says that your can't put into a matrix that stores integers, a char like "*", and infact you can't.
Edit: a possible solutions will be changing your matrix from int to string (parsing integers to strings) as #Some Programmer Dude suggested, but that depends on your needs
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I was solving a problem https://codeforces.com/contest/489/problem/B
Its a simple brute force approach,In my terminal when i am giving input
#include<bits/stdc++.h>
using namespace std;
vector <int> b;
vector <int> g;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
b.push_back(a);
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a;
cin >> a;
g.push_back(a);
}
sort(b.begin(), b.end());
sort(g.begin(), g.end());
int ans = 0;
bool visited[10000];
memset(visited, sizeof(visited), false);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!visited[j])
if (abs(b[i] - g[j]) <= 1) {
visited[j] = true;
ans++;
break;
}
}
}
cout << ans;
}
4
1 4 6 2
5
5 1 5 7 9
I am getting correct output as 3 , This is the very first test case on codeforces also and codeforces showing output as 2 and as showing as wrong answer.
Please see here Proof ,I never faced such kind of problem in competitive
programming .
accepted solution solution
There was also announcement related to this ques read below here
This is a case of undefined behavior: if(!visited[j]) is undefined. visited is not initialized because the call memset(visited, sizeof(visited), false); is wrong. You are reading uninitialized variables.
The declaration of memset is
void *memset( void *dest, int ch, size_t count );
You are writting 0 times the value 10000 into visited. On your machine this memory was filled with zeros. But on other machines there can be different values.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <iostream>
using namespace std;
int compute_binare_code(int n, int a[][4]){
int i,j;
int bC = 0, p = 1;
for(i = 1;i <= n;i++){
for(j = i+1;j <= n;j++){ //just counting bC
bC += a[i][j]*p;
p =p*2;
}
}
return bC;
}
int main(){
cout << "mata3";
int a[4][4],b[5][5],i,j;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
if( (i+j)%2 == 0)
a[i][j]=0;
else
a[i][j]=1;
cout<<"mata1";
cout<<compute_binare_code(4, a);
cout<<"mata2";
return 0;
}
When i run this program, it doesn't give any error but it runs in background forever. It does not print anything, not even "mata3". Can someone explain me why?
Arrays in C++ are indexed from 0.
Your for loop increments i/j from 1 to 4, but it should be 0 to 3.
See also Accessing an array out of bounds gives no error, why?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to learn SSE instructions and I aspire to multiply two matices. However, when I try to initialize one of them, the program crashes with an
Access violation when typing in location
Here's the code that throws the error:
typedef union{
__m128 vec;
float* afloat;
}u_float;
int main(){
__declspec(align(16)) u_float *mat1;
mat1 = (u_float*)malloc(sizeof(u_float)*4);
for(int i = 0; i < 4; i++)
mat1[i].afloat = (float*)malloc(sizeof(float)*4);
for(int i = 0; i < 4; i++)
for(int j = 0; i < 4; j++)
mat1[i].afloat[j] = 1; // Error.
return 0;}
Why is it throwing that error?
And which is the best way to resolve the problem?
This has nothing to do with unions. You have typo in your loop:
for(int i = 0; i < 4; i++)
for(int j = 0; i < 4; j++) // <-- Here
mat1[i].afloat[j] = 1;
Notice that the inner for loop loops while i is less than 4, not when j is less than 4, so this loops infinitely.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());