473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

modifying a container outside function

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<doubleva, 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<doubletmp1(va.size());
valarray<doubletmp2(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<doubleaa, bb;
get_data(filename, &aa, &bb, 1);
}
Aug 18 '06 #1
2 1236
Gary Wessle wrote:
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<doubleva, 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<doubletmp1(va.size());
valarray<doubletmp2(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
There's got to be a better way to do this, but don't you mean:

copy(va.begin(), va.end(), &((*a)[0]));
copy(vb.begin(), vb.end(), &((*b)[0]));

After all, in other words, I think you have inappropriately assumed
that a pointer to a valarray is the same as a pointer to the first
element of the valarray. What makes you think that? (That's a real
question - it may well be that you know something about valarrays that
I don't.) Anyway, I suspect that that is the source of your segfault.

Best regards,

Tom

Aug 18 '06 #2
Gary Wessle <ph****@yahoo.comwrites:
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<doubleva, 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<doubletmp1(va.size());
valarray<doubletmp2(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<doubleaa, bb;
get_data(filename, &aa, &bb, 1);
}

Comment from samj
Date: 08/18/2006 04:30PM PDT
Your Comment

the problem fixed:

here is what I did
void fxpair::get_data(string file, valarray<double>* a,
valarray<double>* b, int x){
ifstream in(file.c_str());
string line;
vector<doubleva, 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() );
valarray<doublet1(va.size());
valarray<doublet2(vb.size());
copy( va.begin(), va.end(), &t1[0] );
copy( vb.begin(), vb.end(), &t2[0] );
if(x){
(*a) = 1.0 / t1;
(*b) = 1.0 / t2;
} else {
(*a) = t1;
(*b) = t2;
}
}

here again after changing the version so it uses refrence instead of pointers.
no it works fine.

void fxpair::get_data(string file, valarray<double>& a,
valarray<double>& b, int x){
ifstream in(file.c_str());
string line;
vector<doubleva, 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() );
valarray<doublet1(va.size());
valarray<doublet2(vb.size());
copy( va.begin(), va.end(), &t1[0] );
copy( vb.begin(), vb.end(), &t2[0] );
if(x){
a = 1.0 / t1;
b = 1.0 / t2;
} else {
a = t1;
b = t2;
}
}

and to be called like this
valarray<doublea1, b1;
get_data(file1, a1, b1);
Aug 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
3
by: Tony | last post by:
So, here is the (back of a cigarette packet) spec for a new UserControl I am trying to develop - 1. It needs to be a control container 2. Scroll bars should appear if any contained control is...
5
by: TheFerryman | last post by:
How bad is it to include a non const accessor to a container of objects? For instance template <class WheelType> class CarBase { typedef std::list<WheelType*> WheelList; private:
7
by: William Payne | last post by:
Hello, have you seen a recent files menu in a GUI application? In many GUI applications there's a menu the displays the most recent files that has been opened by the program. Say such a menu has...
3
by: Chris | last post by:
I have a dataset that I want to modify the values in a particular column. I.e if I have a function to convert the enterbyID (Integer) to a name, in the dataset the field (Concern_EnteredbyID) shows...
0
by: chrispforr | last post by:
I created a container div to hold all the index page’s content. I’m satisfied how the content is displayed within the container div; however, outside the container div, extra blank browser space...
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf on a web page, for instance. I would like to...
4
by: Sean | last post by:
I have a situation whereby I need to modify the text string appearing on an ASP button with some text derived from a Javascript function. But I am unsure of the correct syntax to do so. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.