Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: size of C++ class question...

Author: Eugene Nalimov

Date: 13:53:16 02/14/99

Go up one level in this thread


On February 14, 1999 at 05:55:42, Bo Persson wrote:

>On February 14, 1999 at 01:53:23, James Long wrote:
>
>>
>>The use the following class in one of my programs.
>>I'm trying to find ways to trim it down to four
>>bytes, so that I can do a UNION with an int for
>>quick comparisons.
>>
>>A quick summation of the size of the private
>>variables comes up to 9 bytes. However, the actual
>>size of this class is 12 bytes.
>>
>>Could somebody explain where the extra 3 bytes
>>comes from? (Is it the enumeration, or the constructor?
>>Do the functions add to the size??)
>>
>>Even if I removed the score field and used the high
>>order bits of the source, dest, captured, and mover
>>fields to represent special moves, I would still be a 7
>>or so bytes.
>>
>>Suggestions on reducing the size of this thing
>>would be greatly appreciated.  As you can see, the
>>overloaded operators '==' and '!=' are very
>>inefficient.
>>
>>--
>>James
>>
>
>This is my way of packing the info in 4 bytes. It uses an MSVC extension that
>allows unnamed unions, but you get the general idea:

Anonymous unions are part of ANSI C; they are not MS extension.
I suspect that anonymous structs are, but not sure (have no
standard nearby).

Eugene

>   struct BaseMove
>   {
>      union
>      {
>         struct
>         {
>            unsigned   m_FromSquare         : 8;
>            unsigned   m_ToSquare           : 8;
>            unsigned   m_MovedPiece         : 4;
>            unsigned   m_PromotedPiece      : 4;
>            unsigned   m_Captured           : 4;
>            unsigned   m_IsChecking         : 1;
>            unsigned   m_IsMating           : 1;
>            unsigned   m_IsEnPassantCapture : 1;
>            unsigned   m_IsCastling         : 1;
>         };
>         unsigned __int32   All;
>      };
>
>   };
>
>   class Move : public BaseMove
>   {
>      // Construction
>   public:
>      explicit Move()
>      { };
>
>      Move(const BaseMove CopyFrom)
>      { All = CopyFrom.All; }
>
>      // Helpers
>
>      bool operator == (const BaseMove CompareTo) const
>      { return All == CompareTo.All; }
>
>      bool operator != (const BaseMove CompareTo) const
>      { return All != CompareTo.All; }
>
>      etc...
>   }
>
>
>
>Bo Persson
>bop@malmo.mail.telia.com



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.