473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

a question about memory error..

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
1 1379

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PhoneJack | last post by:
I am writting an algorithm that generates very large tables in dynamic memory on a linux system. These 2 tables are of the order of (30,000 x 30,000). They are being generated by the following...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
50
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
17
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
21
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.