473,756 Members | 3,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GenerateRandomP ositions(vector <double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeu rons;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 4736
"Ivan Paganini" <iv**********@y ahoo.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 GenerateRandomP ositions(vector <double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeu rons;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 'GenerateRandom Positions' 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.0 310040650.3107c 228
@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 GenerateRandomP ositions(
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;

GenerateRandomP ositions( px, py, 10, ss );

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

std::copy(
py.begin(), py.end(),
std::ostream_it erator< 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 GenerateRandomP ositions(vector <double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
posiY.clear();

for(i=0;i<NbNeu rons;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
2270
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 foo containing vectors of the size 3. I then want to compare a vector<double> of size 3 (coord) with foo to find a sequence of elements in foo that equals coord. However, when I try this it returns when just one of the element of coord equals one...
2
11541
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
17831
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
10
15790
by: bluekite2000 | last post by:
and why doesnt the standard vector have such conversion available?
14
2666
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
12376
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 :-). #include <iostream> #include <vector> using namespace std;
9
8293
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 `>' token in that line
32
4027
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 large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9841
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.