By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

Problem with vector<double> alocation

P: n/a
Hello all.
I'm trying to use a vector<double> to alocate de data that i need, and
I am passing by reference to the function to receive the values. The
code goes like this:
//code
void GenerateRandomPositions(vector<double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeurons;i++)
{
posiX.push_back((double)(rand()%sizespace[0]+1));
posiY.push_back((double)(rand()%sizespace[1]+1));
}
}

//end code

where i need the values of posiX and posiY returned to the program.
The size of NbNeurons is about 10, and sizespace is a number that the
program gets from the number of coordinates in my database. When i try
to run the program, i get the segmentation fault. What can be the
problem?

Thanks.

Ivan S>P<M
Grupo de Visão Cibernética - IFSC - USP - Br
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Ivan Paganini" <iv**********@yahoo.com.br> wrote...
Hello all.
I'm trying to use a vector<double> to alocate de data that i need, and
I am passing by reference to the function to receive the values. The
code goes like this:
//code
void GenerateRandomPositions(vector<double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeurons;i++)
{
posiX.push_back((double)(rand()%sizespace[0]+1));
posiY.push_back((double)(rand()%sizespace[1]+1));
}
}

//end code

where i need the values of posiX and posiY returned to the program.
The size of NbNeurons is about 10, and sizespace is a number that the
program gets from the number of coordinates in my database. When i try
to run the program, i get the segmentation fault. What can be the
problem?


The problem is most likely in 'sizespace'. Since you don't show
how you call your 'GenerateRandomPositions' function, there is no
way to tell what specifically is going on.

BTW, using '%' with the result of calling 'rand' is not a good idea.
See comp.lang.c FAQ for the explanation why and what to use instead.

Victor
Jul 19 '05 #2

P: n/a
Ivan Paganini wrote in news:ac0d0790.0310040650.3107c228
@posting.google.com:
Hello all.
I'm trying to use a vector<double> to alocate de data that i need, and
I am passing by reference to the function to receive the values. The
code goes like this:
//code


#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
#include <algorithm>

using std::vector;

void GenerateRandomPositions(
vector<double>& posiX, vector<double>& posiY,
int nbneurons, int* sizespace
)
{
posiX.clear();
posiY.clear();

for( int i=0; i<nbneurons; i++)
{
posiX.push_back((double)(rand()%sizespace[0]+1));
posiY.push_back((double)(rand()%sizespace[1]+1));
}
}
int main()
{

int ss[2] = { 100, 200 };
vector< double > px, py;

GenerateRandomPositions( px, py, 10, ss );

std::copy(
px.begin(), px.end(),
std::ostream_iterator< double >( std::cout, "," )
);
std::cout << "\n";

std::copy(
py.begin(), py.end(),
std::ostream_iterator< double >( std::cout, "," )
);
std::cout << std::endl;
}


where i need the values of posiX and posiY returned to the program.
The size of NbNeurons is about 10, and sizespace is a number that the
program gets from the number of coordinates in my database. When i try
to run the program, i get the segmentation fault. What can be the
problem?


After fixing 1 error (spelling) and adding main() the above
compiles and runs.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

P: n/a
Ivan Paganini wrote:
Hello all.
I'm trying to use a vector<double> to alocate de data that i need, and
I am passing by reference to the function to receive the values. The
code goes like this:
//code
void GenerateRandomPositions(vector<double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeurons;i++)
{
posiX.push_back((double)(rand()%sizespace[0]+1));
posiY.push_back((double)(rand()%sizespace[1]+1));
}
}

//end code


http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Pay particular attention to items 1 through 3.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.