Problem with vector<double> alocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Paganini

    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
  • Victor Bazarov

    #2
    Re: Problem with vector&lt;doubl e&gt; alocation

    "Ivan Paganini" <ivanpaganini@y ahoo.com.br> wrote...[color=blue]
    > 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?[/color]

    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


    Comment

    • Rob Williscroft

      #3
      Re: Problem with vector&lt;doubl e&gt; alocation

      Ivan Paganini wrote in news:ac0d0790.0 310040650.3107c 228
      @posting.google .com:
      [color=blue]
      > 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
      >
      >[/color]

      #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;
      }

      [color=blue]
      >
      > 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?
      >[/color]

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

      HTH

      Rob.
      --

      Comment

      • Kevin Goodsell

        #4
        Re: Problem with vector&lt;doubl e&gt; alocation

        Ivan Paganini wrote:
        [color=blue]
        > 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
        >[/color]



        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.

        Comment

        Working...