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?

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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.