Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a question about memory error..

Reply
Thread Tools

a question about memory error..

 
 
liuhaoran
Guest
Posts: n/a
 
      12-11-2006
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....

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-11-2006

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

Whats so complicated about that?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Patriot Memory PDC1G5600ELK Memory Review Silverstrand Front Page News 0 09-07-2005 02:24 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
RAM Memory or virual memory Julián Sanz García ASP .Net 4 11-12-2004 06:25 PM
GC does not release memory...memory keeps growing!!! Mahesh Prasad ASP .Net 1 02-22-2004 08:40 AM
AspNet Process Memory Issue on Win2k Server - Peformance is fine - Memory usuage doesn't stop growing Cy Huckaba ASP .Net 1 06-26-2003 04:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57