473,399 Members | 3,832 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,399 software developers and data experts.

Problem with vector<double> alocation

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
3 4706
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
10
by: bluekite2000 | last post by:
and why doesnt the standard vector have such conversion available?
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.