0

I have a genetic algorithm program, everything is allocated dynamically using vectors. Nowhere is the number of generations or individuals per generation set at compile time.

I tried it using 500, 1000, 2000 generations, it runs perfect. Then I tried 10,000 generations. It gave me debug assertion failed, vector subscript out of range at generation 4966.

I tried again twice with the same parameters, 10,000 generations, it ran fine.

I tried it once more, I got the error at generation 7565.

It's strange that sometimes it works perfectly, sometimes I get the error. Especially considering that everything is done using vectors.

Any ideas on where could the problem come from? Maybe the debug mode is buggy for some reason?

jazzybazz
  • 1,807
  • 4
  • 17
  • 21
  • 6
    The problem comes from a bug in your code. Without seeing any of your code it's hard to guess what it might be. – john Mar 19 '13 at 13:56
  • Are you zero-initialising your vectors? If not, you could be making decisions based on random data. Perhaps use Valgrind? – RichieHindle Mar 19 '13 at 13:57
  • 2
    "It's strange that sometimes it works perfectly, sometimes I get the error" - no it is not. I have a lot of that with my bugs. please show us the code. – Zdeslav Vojkovic Mar 19 '13 at 13:57
  • 1
    Have you tried running your code inside a debugger? Have you tried to use a memory checker, such as [valgrind](http://valgrind.org)? – Michael Wild Mar 19 '13 at 13:57
  • It's possible that this results from relocation of the vector memory which is only triggered on a certain size, this depends on your STL. Make sure you are not assuming data in hand is valid across vector ops that can invalidate iterators, references. – Steve Townsend Mar 19 '13 at 13:59

1 Answers1

1

The problem comes from stack corruption or most probably from index out of bounds access. The fact that there are cases when your code crashes indicates there is something wrong. If your code is multi-threaded the problem may be because if actions are executed in a given order your code will try to access something out of bounds for a vector.

My advice is to run your code using valgrind and see what it will say. Usually it helps in resolving similar issues.

Also note the fact that there are cases when your code does not crash this does not mean that it works perfectly. You may still have stack corruption or similar.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176