G
Gary Wessle
Hi
I wrote a routine which takes 4 arguments, first a file-name, a text file which has 2
columns of doubles. second and third is a pointer to valarray<double>,
the forth is an int. if the int is true, it give 1/data-read.
it compiled but and ran but did not produce the side effect I want.
thanks for you time.
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <valarray>
using std::valarray;
#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
#include <sstream>
using std::stringstream;
void get_data(string, valarray<double>*, valarray<double>* , int x=0);
void get_data(string file, valarray<double>* a,
valarray<double>* b, int x){
ifstream in(file.c_str());
string line;
vector<double> va, vb;
while( getline(in, line) ){
stringstream input(line.c_str() );
double num1, num2;
while( input >> num1 >> num2 ){
va.push_back(num1);
vb.push_back(num2);
}
}
(*a).resize( va.size() );
(*b).resize( vb.size() );
if(x){
valarray<double> tmp1(va.size());
valarray<double> tmp2(vb.size());
copy( va.begin(), va.end(), &tmp1[0] );
copy( vb.begin(), vb.end(), &tmp2[0] );
(*a) = 1.0 / tmp1;
(*b) = 1.0 / tmp2;
} else {
copy( va.begin(), va.end(), a ); //<------------- here
copy( vb.begin(), vb.end(), b ); //<--------------here
}
}
the lines
****************
copy( va.begin(), va.end(), a );
copy( vb.begin(), vb.end(), b );
****************
caused a segmentation fault.
am I not taking the start of the container which needs to be populated
by using the pointer to it, which is a and b and the sizes are set
correctly, then why the seg faults?
int main(){
valarray<double> aa, bb;
get_data(filename, &aa, &bb, 1);
}
I wrote a routine which takes 4 arguments, first a file-name, a text file which has 2
columns of doubles. second and third is a pointer to valarray<double>,
the forth is an int. if the int is true, it give 1/data-read.
it compiled but and ran but did not produce the side effect I want.
thanks for you time.
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <valarray>
using std::valarray;
#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
#include <sstream>
using std::stringstream;
void get_data(string, valarray<double>*, valarray<double>* , int x=0);
void get_data(string file, valarray<double>* a,
valarray<double>* b, int x){
ifstream in(file.c_str());
string line;
vector<double> va, vb;
while( getline(in, line) ){
stringstream input(line.c_str() );
double num1, num2;
while( input >> num1 >> num2 ){
va.push_back(num1);
vb.push_back(num2);
}
}
(*a).resize( va.size() );
(*b).resize( vb.size() );
if(x){
valarray<double> tmp1(va.size());
valarray<double> tmp2(vb.size());
copy( va.begin(), va.end(), &tmp1[0] );
copy( vb.begin(), vb.end(), &tmp2[0] );
(*a) = 1.0 / tmp1;
(*b) = 1.0 / tmp2;
} else {
copy( va.begin(), va.end(), a ); //<------------- here
copy( vb.begin(), vb.end(), b ); //<--------------here
}
}
the lines
****************
copy( va.begin(), va.end(), a );
copy( vb.begin(), vb.end(), b );
****************
caused a segmentation fault.
am I not taking the start of the container which needs to be populated
by using the pointer to it, which is a and b and the sizes are set
correctly, then why the seg faults?
int main(){
valarray<double> aa, bb;
get_data(filename, &aa, &bb, 1);
}