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?