Problem with vector<double> alocation

I

Ivan Paganini

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
 
V

Victor Bazarov

Ivan Paganini said:
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
 
R

Rob Williscroft

Ivan Paganini wrote in @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::eek:stream_iterator< double >( std::cout, "," )
);
std::cout << "\n";

std::copy(
py.begin(), py.end(),
std::eek:stream_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.
 
K

Kevin Goodsell

Ivan said:
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-lite/how-to-post.html#faq-5.8

Pay particular attention to items 1 through 3.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top