0

I am programming for a genetic algorithm. there is a class named Chromosome.

populationsize=1000;
Chromosome chromosomelist[populationsize];

when I am using this array of objects chromosomelist to initialize some other object

Chromosome parent1 = chromosomelist[p1];

I am getting access violation error for p1 greater than 20 upto 999, while it is getting executed in another function.
what could be the reason? do I need to post my code?

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by ravenous
0

You have declared your chromosomelist array as a stack variable so, depending on the size of a Chromosome object, you could be getting a stack overflow for an array of 1000 objects. If I were you, I'd use a std::vector to store your chromosomes in:

std::vector< Chromosome > vChromosomeList( iPopulationSize );

// ...

Chromosome parent1 = vChromosomeList[ p1 ];

To use std::vector you will have to #include <vector> at the top of your code.

If you don't want to use std::vector , then you can use new to allocate heap memory for your array:

Chromosome* chromosomeList = new Chromosome[ iPopulationSize ];

// ...

Chromosome parent1 = chromosomeList[ p1 ];

// ...

delete[] chromosomeList;   // Don't EVER forget to do this!

However, this is basically what std::vector is doing for you, so I wouldn't bother - just use std::vector .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.