Computer Chess Club Archives


Search

Terms

Messages

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

Author: Bo Persson

Date: 13:08:47 06/12/02

Go up one level in this thread


On June 12, 2002 at 15:49:36, Russell Reagan wrote:

>On June 12, 2002 at 15:40:15, Oliver Roese wrote:
>
>>Simply make sure your constructor is empty and your class has
>>no virtual functions. The compiler will then optimize the call away.
>>I had the same problem recently and this solution worked with gcc.
>
>Someone else said the opposite was true for MS VC++ (which is what I'm using).
>Here's the link: http://www.talkchess.com/forums/1/message.html?234679
>
>He said that MS VC++ will sit in a loop initializing all of the objects in an
>array, even if the constructor is empty. I wonder if maybe he was getting this
>result in DEBUG mode maybe? He didn't say when I asked him, so who knows.

That will be debug mode, definitely!

> Maybe
>MS VC++ does optimize this away in RELEASE mode, which would be problem solved
>:) You'd think surely a compiler as popular as MS VC++ would handle a seemingly
>trivial optimization like this, but once upon a time there was Windows 95 too...
>
>Russell

Here is your example again

// testmove.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class Move
{ };

class MoveList {
    Move  moves[512];
    int   numMoves;
  public:
    // constructors and other methods here
     void add(const Move& NewMove)
     { moves[numMoves++] = NewMove; }
};

void f()
{
   MoveList   x;

   x.add(Move());
}

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

__________________

I have added an operation to the list, because otherwise x would be removed
entirely...

For MSVC++ 7.0, The code for f() looks like this

PUBLIC  ?f@@YAXXZ                                       ; f
; Function compile flags: /Ogty
;       COMDAT ?f@@YAXXZ
_TEXT   SEGMENT
$T1363 = -517
_x$ = -516
?f@@YAXXZ PROC NEAR                                     ; f, COMDAT

; 20   :    MoveList   x;
; 21   :
; 22   :    x.add(Move());

  00000 8b 44 24 fc      mov     eax, DWORD PTR _x$[esp+512]
  00004 8a 8c 24 fb fd
        ff ff            mov     cl, BYTE PTR $T1363[esp]
  0000b 81 ec 08 02 00
        00               sub     esp, 520               ; 00000208H
  00011 88 4c 04 04      mov     BYTE PTR _x$[esp+eax+520], cl

; 23   : }

  00015 81 c4 08 02 00
        00               add     esp, 520               ; 00000208H
  0001b c3               ret     0
?f@@YAXXZ ENDP                                          ; f

That's it!

I especially notice that line 20 doesn't generate any code at all except for the
stack space allocation (sub esp,520).


So much for C++ code bloat!  :-)


Bo Persson
bop2@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.