Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Solving the "constructor" problem in C++

Author: Odd Gunnar Malin

Date: 13:13:52 06/12/02

Go up one level in this thread


On June 12, 2002 at 15:09:30, Russell Reagan wrote:

>After making my decision to use C++ instead of C, I thought I had most of the
>drawbacks covered, or at least I was content with the drawbacks that I hadn't
>solved. One drawback that I didn't think of was that the constructors for an
>object would run even if they did nothing. So when I create an array of moves in
>my alpha-beta function, the constructor will run many times, and when done
>recursively, that's a bad thing.
>
>I saw several people post workarounds using malloc() or other slightly cryptic
>hacks that I'd rather avoid, and these are the ideas I've come up with so far to
>try and solve this problem.
>
>My first idea was to use an STL vector instead of an array, and since a vector
>wouldn't actually contain any objects when it's initialized, the constructors
>shouldn't run right? My solution would require that the objects had proper
>constructors so that I could do something like the following:
>
>vector<Move> legalMoves;
>legalMoves.push_back( Move(from,to) );
>
>That would allow me to get the correct move into the vector without having to
>"waste" constructor calls.
>
>My second idea was to use a struct instead of a class for things that will be
>created on the fly like Move. If I did this, I could still add methods to the
>Move struct in C++, but as far as I know I don't have to write a constructor,
>and then I could only initialize the data manually.
>
>So will either of these solutions work, or does anyone have any other
>suggestions?
>
>Thanks,
>Russell

Why not have movelists initialize at startup?

class Search
 Move nullmoves[MAX_PLY];
 Move hashmoves[MAX_PLY];
 MoveList generated_moves[MAX_PLY];

  int alphabeta(..)
  {
    getHashmove(hashmoves[ply]);
    ....
    donullmove(nullmove[ply]);
    ....
    GenMoves(generated_moves[ply]);
    ....
  }

The MoveList could also have an index (size) that point to next element so you
only need to set size=0 on generated_moves[ply].clear();

It possible that all move lists should be in a global pool for speed (i have not
tested this).

Odd Gunnar



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.