Just started using cLion as an ide for c . Am attempting to run this block of code where it reads a csv file and stores the values in a 189 x 141 2d vector. It then iterates through 5 for loops. Everything was running smoothly until I included the 5 nested for loops; at this point I was seeing the "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)" message upon executing. I have seen an answer to a similar question claiming that it is the result of a lack of memory on my computer. Would this be the case?
When the first 3 for loops have ib0, ib1, ib2 iterate from 0 to 100, it would of course mean that there are over 26 billion iterations in total. However, when I reduce this range to 0 to 1, I still receive the message.
For reproducibility, I just have the ROB vector be 189 x 141 random values.
EDIT: If anyone runs this code, let me know how long it takes?
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
double innerarrayval;
vector<vector<double>> ROB;
vector<double> innerarray;
for(int x=0;x<189;x ) {
innerarray.clear();
for (int y = 0; y < 141; y ) {
innerarrayval = rand() % 1000;
innerarray.push_back(innerarrayval);
}
ROB.push_back(innerarray);
}
double b0,b1,b2;
int nnb;
double chisquared, amean, content, sumpart, sumpartsquared,chisquaredNDOF,chimin;
double b0mem, b1mem, b2mem;
chimin = 1000.0;
for(int ib0 = 0; ib0 < 101; ib0 )
{
b0 = 15.0 0.1 * (ib0 - 1);
for(int ib1 = 0; ib1 < 101; ib1 )
{
b1 = -0.002 * (ib1 - 1);
for(int ib2 = 0; ib2 < 101; ib2 )
{
b2 = 0.002 * (ib2 - 1);
nnb = 0;
chisquared = 0;
amean = 0;
for(int i = 0; i <= 189; i )
{
for(int j = 0; j <= 141; j )
{
if((i >= 50 and i <= 116) and (j >= 42 and j <= 112))
{
continue;
}
else
{
content = ROB[i][j];
if(content == 0)
{
content = 1;
}
amean = amean content;
sumpart = (content - b0 - (b1 * i) - (b2 * j))/sqrt(content);
sumpartsquared = pow(sumpart, 2);
chisquared = chisquared sumpartsquared;
nnb = nnb 1;
}
}
}
chisquaredNDOF = chisquared/double(nnb);
amean = amean/double(nnb);
if(chisquaredNDOF < chimin)
{
chimin = chisquaredNDOF;
b0mem = b0;
b1mem = b1;
b2mem = b2;
}
}
}
}
cout<<"chi squared: "<<chimin<<"\n";
cout<<"b0: "<<b0mem<<"\n";
cout<<"b1: "<<b1mem<<"\n";
cout<<"b2: "<<b2mem<<"\n";
cout<<"mean: "<<amean<<"\n";
return 0;
}
CodePudding user response:
for(int x=0;x<189;x ) {
innerarray.clear();
for (int y = 0; y < 141; y ) {
innerarrayval = rand() % 1000;
innerarray.push_back(innerarrayval);
}
ROB.push_back(innerarray);
}
This part of the initialization loops carefully, and gingerly, initialized the two-dimensional ROB
vector. As noted by each for
loop's limit, the valid indexes for the first dimension is 0-188, and the 2nd dimension is 0-140.
This is correct. In C array/vector indexes start with 0, so for your expected result of a "189 x 141 2d vector", the first index's values range 0-188, and the 2nd one's range is 0-140.
If you proceed further in your code, to the part that reads the two-dimensional matrix:
for(int i = 0; i <= 189; i )
{
for(int j = 0; j <= 141; j )
{
And because this uses <=
, this will attempt to access values in ROB
whose first dimension's index range is 0-189 and the 2nd dimension's index range is 0-141.
The out of bounds access results in undefined behavior, and your likely crash.
You should use this obvious bug as an excellent opportunity for you to learn how to use your debugger. If you used your debugger to run this program, the debugger would've stopped at the point of the crash. If you then used your debugger to inspect the values of all variables, I would expect that the out-of-range values for i
or j
would be very visible, making the bug clear.