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 6 days ago.
Improve this question
int number_of_elements = 5;
std::shared_ptr<int[]> dataholder;
dataholder = std::make_shared<int[]>(number_of_elements);
for (int i = 0; i < number_of_elements; i++)
dataholder[i] = rand() % 100; // -----> This line
for (int i = 0; i < number_of_elements; i++)
std::cout<< dataholder.get()[i] <<'\n';
printf("Success");
I want the smart point of type int[] to contain 5 random integers,
but I have an issue with this line
dataholder[i] = rand() % 100;
it causes
malloc(): corrupted top size
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
How can I do to fix this issue?
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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'm working on a hangman game for a college asignment and i aleady have most of the work done but for some reason this part here is not working. Is there a problem with my logic? It seems to be incredibly simple.
bool second_check(char user_input) {
char u[3]={'a','r','i'};
for (int i = 0; i <= 3; i++) {
if (user_input==u[i]){
return true;
};
};
return false;
}
int main(){
char o;
cout<<"enter"<<endl;
cin>>o;
if (second_check(o)==true) {
cout << "Correct!" << endl;
}
else
cout << "Wrong! \n Strike one!" << endl;
return 0;
}
The for loop will loop 4 times even though you have 3 elements, causing it to try and access an undefined location in memory, to fix this replace the 'i <=3' with 'i<3'
so the for loop should like this in the end:
for (int i = 0; i < 3; i++) {
if (user_input==u[i]){
return true;
};
};
Answer
Since char u[3]={'a','r','i'}; contains only 3 characters, your for loop will be:
for (int i = 0; i <= 2; i++) or
for (int i = 0; i < 3; i++).
Explanation
This is because, in C/C++ and most of the programming language, the array count starts from 0. Therefore the first element will be array[0] and last will array[n-1] where n is the size of the array used at initialization. (Above, n=3)
So it would be helpful to the community if you state clearly what the problem is (i.e expected vs. actual output).
That said, a couple problems I can see...
for (int i = 0; i <= 3; i++)
if (user_input==u[i]){
Since u is of size 3 (char user[3]), you need to change your for loop to be i < 3 since arrays are 0 based, valid indices are 0,1,2 and you will go out of bounds of the array. I.e. user[3] is not a valid index.
You are not comparing an index of user_input which I suspect you want to. i.e. user_input[i].
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());