By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

a question about memory error..

P: n/a
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....

Dec 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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::ostream_iterator< float >(std::cout, "\n") );
}

Whats so complicated about that?

Dec 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.