c++ - Is my array the cause of rapid cpu & memory build up? -


my mcvc compiling not functioning intended. goal genetic algorithm performs basics i.e. crossover,mutation,evolution. in code have provided should print out job statements thats not case. new programmer, sorry. questions are:

1) cpu , ram rev'ed up, array declaration , implementation cause uncontrolled spike?

std::array<std::auto_ptr<individual>,50>myarray; 

2) 2.53 ghz intel core 2 duo not it?

3) should cut down amount of loops?

any welcomed !

individual.h

#include <stdio.h> #include <cstdlib> #include <ctime> #include <vector> #include <array> #include <iostream> class individual { public:      inline int getrandomnumber(int min = 0, int max = 1)     {         srand(static_cast<unsigned int>(time(0)));         static const double fraction = 1.0 / (static_cast<double>(rand_max) + 1.0);         return static_cast<int>(rand() * fraction * (max - min + 1) + min);     } private:     int defaultgenelength = 64;     std::vector<char>genes;     int fitness = 0;  public:     individual()     {         std::cout<<"good job";      }      //setters , getters     void generateindividual();     void setdefaultgenelength(int length);     char getgene(int index);     void setgene(int index, char value);      //public methods     unsigned int size();     int getfitness();     std::string tostring(); }; 

individual.cpp

#include "individual.h"  void individual::generateindividual() {     (int = 0; < size(); i++)     {         genes.push_back(getrandomnumber());     } }   //setters , getters void individual::setdefaultgenelength(int length) {     defaultgenelength = length; }   char individual::getgene(int index) {     return genes.at(index);  }   void individual::setgene(int index, char value) {     genes[index] = value;     fitness = 0; }   //public methods unsigned int individual::size() {     return genes.max_size(); }   int individual::getfitness() {     if(fitness == 0)     {         fitness = 1;      }   return fitness;  }   std::string individual::tostring() {     std::string genestring = "";     (int = 0; < size(); i++)     {         genestring.append(getgene(i),1);     }     return genestring;  } 

population.h

#include "individual.h"  class population { std::array<std::auto_ptr<individual>,50>myarray;  public:       population(int populationsize, bool initialise)     {         std::cout<<"good job2";         if(initialise)         {             (int = 0; < populationsize; ++i)             {                 std::auto_ptr<individual>newindividual(new individual());                 myarray.at(i) = newindividual;                 myarray.at(i)->generateindividual();                 saveindividual(i,*(myarray.at(i)));             }         }         std::cout<<"good job 3";     }      individual getindividual(int index);     individual getfittest();     unsigned long size();     void saveindividual (int index, individual indiv);      ~population()     {      } }; 

population.cpp

#include "population.h" individual population::getindividual(int index) {     return *myarray.at(index); }  individual population::getfittest() {     individual fittest = *myarray.at(0);      (int = 0; < myarray.max_size(); i++)     {         if (fittest.getfitness() <= getindividual(i).getfitness())         {             fittest = getindividual(i);         }     }     return fittest; }  unsigned long population::size() {     return myarray.max_size(); }  void population::saveindividual (int index, individual indiv) {     *myarray.at(index) = indiv; } 

main.cpp

int main(int argc, const char * argv[]) {      population *mypop = new population(2,true);     delete mypop;     mypop = nullptr; return 0; } 

unsigned int individual::size() {     return genes.max_size(); } 

your genes a:

std::vector<char> genes; 

the c++ standard defines std::vector::max_size() follows:

distance(begin(), end()) largest possible container

it not specified "largest possible container" means. "possible" mean anything, like, if system had ten terabyte hard drive, operating system use entire hard drive page virtual memory address space. that's "possible", in sense of word. goes without saying paging out ten terabytes take while.

with 64 bit gcc, following simple program:

#include <iostream> #include <vector>  int main() {     std::vector<char> c;      std::cout << c.max_size() << std::endl;     return 0; } 

produces following output:

18446744073709551615 

however, chances of being able create vector of such size not good.

but let's code:

void individual::generateindividual() {     (int = 0; < size(); i++)     {         genes.push_back(getrandomnumber());     } } 

ok. must feeling quite lucky. believe possible create vector<char> that's max_size() big.

are quite sure that?

i'm skeptical.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -