a question about memory error..

Discussion in 'C++' started by liuhaoran, Dec 11, 2006.

  1. liuhaoran

    liuhaoran Guest

    HI. i have a question about memory error.

    when i change double variable to float variable ,for example:

    int curGen = 0;
    double sum = 0;

    // m_iPopSize is int variable ,NewPop is a vector
    int NumToAdd = m_iPopSize - NewPop.size();

    double PointerGap = m_dTotalFitnessScore/(double)NumToAdd;

    //here !!change double to float, RandFolat() return a
    double variable .
    float ptr = RandFloat() * PointerGap;

    while (NewPop.size() < NumToAdd)
    {
    assert(curGen < m_vecGenomes.size());

    //m_vecGenomes is a vector
    for (sum+=m_vecGenomes[curGen].dFitness; sum>ptr; ptr+=PointerGap)
    {
    NewPop.push_back(m_vecGenomes[curGen]);

    if (NewPop.size() == NumToAdd)
    {
    return;
    }
    }

    ++curGen;
    }


    when runned for 15000 times ,memory error occurs.
    This is why?
    And when using double ,no error....
    liuhaoran, Dec 11, 2006
    #1
    1. Advertising

  2. liuhaoran

    Salt_Peter Guest

    liuhaoran wrote:
    > HI. i have a question about memory error.
    >
    > when i change double variable to float variable ,for example:
    >
    > int curGen = 0;
    > double sum = 0;
    >
    > // m_iPopSize is int variable ,NewPop is a vector
    > int NumToAdd = m_iPopSize - NewPop.size();
    >
    > double PointerGap = m_dTotalFitnessScore/(double)NumToAdd;
    >
    > //here !!change double to float, RandFolat() return a
    > double variable .
    > float ptr = RandFloat() * PointerGap;
    >
    > while (NewPop.size() < NumToAdd)
    > {
    > assert(curGen < m_vecGenomes.size());
    >
    > //m_vecGenomes is a vector
    > for (sum+=m_vecGenomes[curGen].dFitness; sum>ptr; ptr+=PointerGap)
    > {
    > NewPop.push_back(m_vecGenomes[curGen]);
    >
    > if (NewPop.size() == NumToAdd)
    > {
    > return;
    > }
    > }
    >
    > ++curGen;
    > }
    >
    >
    > when runned for 15000 times ,memory error occurs.
    > This is why?
    > And when using double ,no error....


    error: 'm_iPopSize' was not declared in this scope
    error: 'NewPop' was not declared in this scope
    error: 'm_dTotalFitnessScore' was not declared in this scope
    error: expected initializer before '.' token
    error: expected unqualified-id before 'while'

    and i'm surprised the compiler is skipping other inconsistancies.
    Some of which are disturbing to say the least.

    If i needed to post code that both compiles and loads a vector of
    floats with a set of not so random numbers:

    // #include <cstdlib>
    #include <iostream>
    #include <ostream>
    #include <vector>
    #include <iterator>

    template< typename T >
    void
    add_random_element(const int range, std::vector< T >& r_vt)
    {
    r_vt.push_back( static_cast< T >( rand() % range) );
    }

    int main()
    {
    std::vector< float > vf;
    for( size_t i; i < 1500; ++i )
    {
    add_random_element( 100, vf );
    }

    std::copy( vf.begin(),
    vf.end(),
    std::eek:stream_iterator< float >(std::cout, "\n") );
    }

    Whats so complicated about that?
    Salt_Peter, Dec 11, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mahesh Prasad
    Replies:
    1
    Views:
    690
    Tom Wells
    Feb 22, 2004
  2. Cy Huckaba
    Replies:
    1
    Views:
    1,133
    Xie Xiao
    Jun 26, 2003
  3. Julián Sanz García

    RAM Memory or virual memory

    Julián Sanz García, Nov 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    882
    Julián Sanz García
    Nov 12, 2004
  4. humbleaptience
    Replies:
    0
    Views:
    5,472
    humbleaptience
    Feb 22, 2006
  5. Todd
    Replies:
    4
    Views:
    517
    Jeff Higgins
    Sep 5, 2007
Loading...

Share This Page