[computer-go] C++ pointer question
Weston Markham
weston.markham at gmail.com
Tue Aug 8 14:03:34 PDT 2006
Whoa! I believe that this is quite bad advice. These are low-level
functions, to be called many, many times over the course of a run.
There should not be any dynamic memory allocated or freed in dilate()!
(Especially not if an automatic variable will accomplish the same
thing.) I would even guess that there should not be any dynamic
memory allocated or freed in the constructor & destructor, either,
although that may be less of a problem, given that it provides some
flexibility at run time.
Weston
On 8/8/06, Berk Ozbozkurt <ozbozkurt at fisek.com.tr> wrote:
> Peter Drake wrote:
>
> > Okay, I've got this method in my BitBoard class:
> >
> > void dilate() // Expand to include all adjacent points
> > {
> > BitBoard temp;
> > temp.bits[0] = bits[0] | (bits[0] << 1) | (bits[0] >> 1) | bits[1];
> > temp.bits[board_width - 1] = bits[board_width - 1] | (bits
> > [board_width - 1] << 1)
> > | (bits[board_width - 1] >> 1) | bits[board_width - 2];
> > for (unsigned row = 1; row < board_width - 1; row++) {
> > temp.bits[row] = bits[row] | (bits[row] << 1) | (bits[row] >>
> > 1) | bits[row - 1] | bits[row + 1];
> > }
> > for (unsigned row = 0; row < board_width; row++) {
> > bits[row] = temp.bits[row];
> > }
> > }
> >
> > Is there some way to avoid that second for loop? I tried "this =
> > temp" and "*this = *temp", but the compiler isn't happy with either.
> >
> It has been quite a while since I used C++, but IIRC (and assuming you
> have not overloaded [, ], << and >> operators) you can store your data
> (bits) in a dynamic array. In that case a simple assignment will work:
>
> class Bitboard{
> bits_type *bits;
> Bitboard()
> {
> bits=new bits_type[ARRAY_SIZE];
> ....
> }
> ~Bitboard()
> {
> delete[] bits;
> ....
> }
> void dilate() // Expand to include all adjacent points
> {
> bits_type *temp=new bits_type[ARRAY_SIZE];
> temp.bits[0] = bits[0] | (bits[0] << 1) | (bits[0] >> 1) | bits[1];
> ...
> delete[] bits;
> bits=temp;
> }
> }
>
> }
> _______________________________________________
> computer-go mailing list
> computer-go at computer-go.org
> http://www.computer-go.org/mailman/listinfo/computer-go/
>
More information about the computer-go
mailing list