B
Bryan
Hello all. I'm fairly new to c++. I've written several programs
using std::vectors, and they've always worked just fine. Until today.
The following is a snippet of my code (sorry, can't include all of it-
it's over 1k lines long). In addition, I'm including an "include"
file where structures like "stack" are defined. Again, it's really
long. I doubt the problem lies there, though, because the include
file is used in many other programs, all of which work fine.
----------------------------------------------------------------------------------
//Removed a lot of bullshit before this code.
//Initialize variables.
stack_ignored=0;//Contains the num. of stack data pts not written
to.
j=0;//Counter that holds the number of data points in current swath.
k=0;//Counter that holds current number of nearby "swaths".
multiple_swaths_indices.push_back(temp_vector_long);//Initialize.
multiple_swaths_indices[k].push_back(0);//Add first index.
j++;
int new_set_of_swaths=0;//Set to 1 when a new set of swaths is
detected.
//Loop through stack data starting at the second data point.
for(i=1;i<stack.numpoints;i++){
//Is this data point in the same swath as the last one?
if((stack.time - stack.time[multiple_swaths_indices[k][j-1]])
<= mcd){
multiple_swaths_indices[k].push_back(i);
j++;
}
if((stack.time - stack.time[multiple_swaths_indices[k][j-1]]) >
mcd or i==stack.numpoints-1){
//If not in the same swath, is the previous swath too small?
if(j<min_swath_pts){
//Ignore the incomplete swath.
stack_ignored += j;
cout << "Ignoring " << j << " data points (incomplete swath).
\n";
//Decrement k because reset procedure will increment it.
k--;
//Remove current swath points.
multiple_swaths_indices.pop_back();
//Reset variables for a new swath.
reset_for_new_swath(i, j, k, multiple_swaths_indices);
cout <<"Successfully reset for new swath!\n";
}
//If not, reset for new swath.
else{
//Reset variables for a new swath.
reset_for_new_swath(i, j, k, multiple_swaths_indices);
cout <<"Successfully reset for new swath!\n";
}
if(k>0){
if((stack.time - stack.time[multiple_swaths_indices[k-1]
[multiple_swaths_indices[k-1].size()-1]]) > msd)
new_set_of_swaths=1;
}
else new_set_of_swaths=1;
if(i==stack.numpoints-1) new_set_of_swaths=1;
if(new_set_of_swaths==1){
if(k>0)
if(i<stack.numpoints-1)
cout << "Pt at i="<<i<<" starts a new set of swaths.
Previous index (from msi) is: "<<multiple_swaths_indices[k-1]
[multiple_swaths_indices[k-1].size()-1]<<endl;
else
if(i<stack.numpoints-1)
cout << "Pt at i="<<i<<" starts a new set of swaths.
Previous index is not available.\n";
if(i==stack.numpoints-1)
cout << "Pt at i="<<i<<" is the end of the stack file.\n";
if(j<min_swath_pts){
//Ignore the incomplete swath.
stack_ignored += j;
cout <<"Problem: Ignoring "<<j<<" data points (incomplete
swath).\n";
//Decrement k because that swath is being ignored.
k--;
//Remove current swath points.
multiple_swaths_indices.pop_back();
//Set j to the size of msi[k].
j = multiple_swaths_indices[k].size();
}
//Keep collecting swaths until too great a gap exists.
//Only fit and remove once/rev if there are "enough" swaths.
if(k >= N-1){
//Reset variables for a new SET of swaths.
new_set_of_swaths=0;
cout <<"About to reset for a new set of swaths.\n";
multiple_swaths_indices.clear();
cout<<"Successfully reset for a new set of swaths.\n";
}//End of "if enough swaths exist" commands
}//End of "if next data point isn't even in a nearby swath".
}//End of "if not contiguous or the last datapoint" if statement.
}//End of loop through stack data.
}//End of main() code- I've cut out LOTS of extraneous bullshit.
int reset_for_new_swath(long &i, long &j,long &k,
vector< vector<long> >
&multiple_swaths_indices){
vector<long> temp_vector_long;
j=0;
k++;
multiple_swaths_indices.push_back(temp_vector_long);
multiple_swaths_indices[k].push_back(i);
j++;
return 0;
}
int reset_for_new_set_of_swaths(long &i, long &j,long &k,
vector< vector<long> >
&multiple_swaths_indices){
vector<long> temp_vector_long;
j=0;
k=0;
multiple_swaths_indices.clear();
multiple_swaths_indices.push_back(temp_vector_long);
multiple_swaths_indices[k].push_back(i);
j++;
return 0;
}
----------------------------------------------------------------------------------
When executed, this code (or, rather, the complete program from which
this code has been snipped) produces the following output:
----------------------------------------------------------------------------------
Ignoring 32 data points (incomplete swath).
Successfully reset for new swath!
Pt at i=744 starts a new set of swaths. Previous index (from msi) is:
711
Before reset for new swath: k: 6 j: 1 msi.size(): 7
Problem: Ignoring 1 data points (incomplete swath).
Ignoring 6 swaths (Need 11 swaths).
Ignoring 58 data points (incomplete swath).
Successfully reset for new swath!
Ignoring 31 data points (incomplete swath).
Successfully reset for new swath!
Pt at i=1929 starts a new set of swaths. Previous index (from msi)
is: 1928
Before reset for new swath: k: 9 j: 1 msi.size(): 10
Problem: Ignoring 1 data points (incomplete swath).
Ignoring 9 swaths (Need 11 swaths).
Pt at i=5716 starts a new set of swaths. Previous index (from msi)
is: 5715
Before reset for new swath: k: 25 j: 1 msi.size(): 26
Problem: Ignoring 1 data points (incomplete swath).
About to reset for a new set of swaths.
Segmentation fault (core dumped)
----------------------------------------------------------------------------------
Notice how the segmentation fault occurs right after "About to reset
for a new set of swaths." and the message "Successfully reset for a
new set of swaths." never appears. I believe this means that the
statement "multiple_swaths_indices.clear();" must be causing the
segmentation fault.
Does anyone know why this is happening? I've been staring at this
code for nearly a week now, and I don't have ANY idea why it's
crashing. I've been inserting cout statements like crazy, trying to
identify anything strange, but everything looks normal as far as I can
tell...
Help!
(Please...)
-Bryan
using std::vectors, and they've always worked just fine. Until today.
The following is a snippet of my code (sorry, can't include all of it-
it's over 1k lines long). In addition, I'm including an "include"
file where structures like "stack" are defined. Again, it's really
long. I doubt the problem lies there, though, because the include
file is used in many other programs, all of which work fine.
----------------------------------------------------------------------------------
//Removed a lot of bullshit before this code.
//Initialize variables.
stack_ignored=0;//Contains the num. of stack data pts not written
to.
j=0;//Counter that holds the number of data points in current swath.
k=0;//Counter that holds current number of nearby "swaths".
multiple_swaths_indices.push_back(temp_vector_long);//Initialize.
multiple_swaths_indices[k].push_back(0);//Add first index.
j++;
int new_set_of_swaths=0;//Set to 1 when a new set of swaths is
detected.
//Loop through stack data starting at the second data point.
for(i=1;i<stack.numpoints;i++){
//Is this data point in the same swath as the last one?
if((stack.time - stack.time[multiple_swaths_indices[k][j-1]])
<= mcd){
multiple_swaths_indices[k].push_back(i);
j++;
}
if((stack.time - stack.time[multiple_swaths_indices[k][j-1]]) >
mcd or i==stack.numpoints-1){
//If not in the same swath, is the previous swath too small?
if(j<min_swath_pts){
//Ignore the incomplete swath.
stack_ignored += j;
cout << "Ignoring " << j << " data points (incomplete swath).
\n";
//Decrement k because reset procedure will increment it.
k--;
//Remove current swath points.
multiple_swaths_indices.pop_back();
//Reset variables for a new swath.
reset_for_new_swath(i, j, k, multiple_swaths_indices);
cout <<"Successfully reset for new swath!\n";
}
//If not, reset for new swath.
else{
//Reset variables for a new swath.
reset_for_new_swath(i, j, k, multiple_swaths_indices);
cout <<"Successfully reset for new swath!\n";
}
if(k>0){
if((stack.time - stack.time[multiple_swaths_indices[k-1]
[multiple_swaths_indices[k-1].size()-1]]) > msd)
new_set_of_swaths=1;
}
else new_set_of_swaths=1;
if(i==stack.numpoints-1) new_set_of_swaths=1;
if(new_set_of_swaths==1){
if(k>0)
if(i<stack.numpoints-1)
cout << "Pt at i="<<i<<" starts a new set of swaths.
Previous index (from msi) is: "<<multiple_swaths_indices[k-1]
[multiple_swaths_indices[k-1].size()-1]<<endl;
else
if(i<stack.numpoints-1)
cout << "Pt at i="<<i<<" starts a new set of swaths.
Previous index is not available.\n";
if(i==stack.numpoints-1)
cout << "Pt at i="<<i<<" is the end of the stack file.\n";
if(j<min_swath_pts){
//Ignore the incomplete swath.
stack_ignored += j;
cout <<"Problem: Ignoring "<<j<<" data points (incomplete
swath).\n";
//Decrement k because that swath is being ignored.
k--;
//Remove current swath points.
multiple_swaths_indices.pop_back();
//Set j to the size of msi[k].
j = multiple_swaths_indices[k].size();
}
//Keep collecting swaths until too great a gap exists.
//Only fit and remove once/rev if there are "enough" swaths.
if(k >= N-1){
//Reset variables for a new SET of swaths.
new_set_of_swaths=0;
cout <<"About to reset for a new set of swaths.\n";
multiple_swaths_indices.clear();
cout<<"Successfully reset for a new set of swaths.\n";
}//End of "if enough swaths exist" commands
}//End of "if next data point isn't even in a nearby swath".
}//End of "if not contiguous or the last datapoint" if statement.
}//End of loop through stack data.
}//End of main() code- I've cut out LOTS of extraneous bullshit.
int reset_for_new_swath(long &i, long &j,long &k,
vector< vector<long> >
&multiple_swaths_indices){
vector<long> temp_vector_long;
j=0;
k++;
multiple_swaths_indices.push_back(temp_vector_long);
multiple_swaths_indices[k].push_back(i);
j++;
return 0;
}
int reset_for_new_set_of_swaths(long &i, long &j,long &k,
vector< vector<long> >
&multiple_swaths_indices){
vector<long> temp_vector_long;
j=0;
k=0;
multiple_swaths_indices.clear();
multiple_swaths_indices.push_back(temp_vector_long);
multiple_swaths_indices[k].push_back(i);
j++;
return 0;
}
----------------------------------------------------------------------------------
When executed, this code (or, rather, the complete program from which
this code has been snipped) produces the following output:
----------------------------------------------------------------------------------
Ignoring 32 data points (incomplete swath).
Successfully reset for new swath!
Pt at i=744 starts a new set of swaths. Previous index (from msi) is:
711
Before reset for new swath: k: 6 j: 1 msi.size(): 7
Problem: Ignoring 1 data points (incomplete swath).
Ignoring 6 swaths (Need 11 swaths).
Ignoring 58 data points (incomplete swath).
Successfully reset for new swath!
Ignoring 31 data points (incomplete swath).
Successfully reset for new swath!
Pt at i=1929 starts a new set of swaths. Previous index (from msi)
is: 1928
Before reset for new swath: k: 9 j: 1 msi.size(): 10
Problem: Ignoring 1 data points (incomplete swath).
Ignoring 9 swaths (Need 11 swaths).
Pt at i=5716 starts a new set of swaths. Previous index (from msi)
is: 5715
Before reset for new swath: k: 25 j: 1 msi.size(): 26
Problem: Ignoring 1 data points (incomplete swath).
About to reset for a new set of swaths.
Segmentation fault (core dumped)
----------------------------------------------------------------------------------
Notice how the segmentation fault occurs right after "About to reset
for a new set of swaths." and the message "Successfully reset for a
new set of swaths." never appears. I believe this means that the
statement "multiple_swaths_indices.clear();" must be causing the
segmentation fault.
Does anyone know why this is happening? I've been staring at this
code for nearly a week now, and I don't have ANY idea why it's
crashing. I've been inserting cout statements like crazy, trying to
identify anything strange, but everything looks normal as far as I can
tell...
Help!
(Please...)
-Bryan