0

Ok.. So I'm working on this code on C++. Code is below..

#include <iostream>
#include <ctime>
#include <string>
#include <vector>
using namespace std;

vector<int> makePerson(int n)
{
vector<int>person;
for(int i=0;i<3;i++)
{
    person.push_back(rand()%255+1);
}
return person;
}    

vector<vector<int> > makeGeneration(int n)
{
vector<vector<int> > generation;
for(int i=0;i<n;i++)
{
    generation.push_back(makePerson(i));
}
return generation;
}

vector<int> createChild(vector<vector<int> > &parentGeneration, double mutationRate)
{
    int maleParent  = (rand()%parentGeneration.size())+1;
int femaleParent = (rand()%parentGeneration.size())+1;
bool checkGender = true;
while(checkGender)
{
    if(maleParent==femaleParent)
    {
        checkGender = true;
    }
    else
    {
        checkGender = false;
        break;
    }
}

vector<int> child;
vector<int> temp;
for(int i=0;i<3;i++)
{
    temp.push_back(parentGeneration.at(maleParent).at(i));
    temp.push_back(parentGeneration.at(femaleParent).at(i));
}

for(int i=0;i<3;i++)
{
    child.push_back(temp.at((rand()%3)+1));
}

temp.clear();

for(int i=0;i<3;i++)
{
    if((mutationRate*100) > (rand()%100)+1)
    {
        child.at(i)=(rand()%255)+1;
    }
}
return child;
}    

int main()
{
int N,k,g;
N=1000;
k=2;
double m=0.05;
double d=0.05;
srand(static_cast<unsigned int>(time(0)));
vector<vector<int> > parentGeneration;
vector<vector<int> > childGeneration;
parentGeneration = makeGeneration(N);

    for(int i=0;i<parentGeneration.size();i++)
{
    for(int j=0;j<3;j++)
    {
        cout<<parentGeneration.at(i).at(j)<<endl;
    }
    cout<<endl;
}

cout<<"reach"<<endl;
for(int i=0;i<(1000);i++)
{
    childGeneration.push_back(createChild(parentGeneration,m));
}

cout<<"***CHILD GENERATION***"<<endl;

for(int i=0;i<childGeneration.size();i++)
{
    for(int j=0;j<3;j++)
    {
        cout<<childGeneration.at(i).at(j)<<endl;
    }
    cout<<"i="<<i<<endl;
    cout<<endl;
}
return 0;
}

So, the errors are:

  1. Microsoft C++ exception: std::out_of_range at memory location 0x002cf6c4.
  2. Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer dec

I think it's some problem with the vector and the random generator because this error is random. It sometimes works till 1000 and sometimes stops in between.

Any suggestions. Really urgent..

PLEASEEEE Thanks in advane :D

gkamani2011
  • 225
  • 1
  • 4
  • 14
  • Inconsistent indentation makes code hard to read. – jogojapan Dec 05 '12 at 05:42
  • 1
    One problem is `int maleParent = (rand()%parentGeneration.size())+1`. You shouldn't add `1` at the end, otherwise you'll get values for `maleParent` that are equal to `parentGeneration.size()` every now and then. That's causing out-of-range errors when you use `parentGeneration.at(maleParent)` later. Similar for `femaleParent`. – jogojapan Dec 05 '12 at 05:46

1 Answers1

2

I faced similar issues with the code. I finally settled on using Microsoft's Application Verifier which allowed me to zero in on the issue. Find Application Verifier and download it. Then run the executable appverifier.exe which is in C:\Windows\System32 folder. Select your executable and enable the memory checking. Then run your code in visual studio as you always do and it should jump at or near where the issue is. It won't do all your work for you, but if you know your code it'll surely help.

Gustavo Litovsky
  • 2,457
  • 1
  • 29
  • 41