Newbie C++ compiling question

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jacobbl
Posts: 80
Joined: Wed Feb 17, 2010 3:57 pm

Newbie C++ compiling question

Post by jacobbl »

Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.

So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.

Regards
Jacob
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: Newbie C++ compiling question

Post by diep »

jacobbl wrote:Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.

So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.

Regards
Jacob
I assume you give it a 'rebuild all' each time.

It's a very common problem you describe. C, C++ or Fortran doesn't really matter there. Realize a chess engine is basically a black box. So it's difficult to debug. In short it's programming errors usually that cause this.

Also it is a good habit when programming C++ to not use too many 'tricks' that Bjarne Stroustrup invented - as he didn't yet invent a technique to program bugfree.

The real good programmers are so good usually because they aren't using anything they don't need. KISS. Keep it simple and stupid.

Program in a manner that is more similar to imperative programming i'd recommend, that already gives enough problems and makes code faster. Avoid difficult to debug and tricky things like templates initially.

After all, if you take a C++ program and look at it from a distance it all looks like C code to me.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Newbie C++ compiling question

Post by mcostalba »

jacobbl wrote: So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.
Try to compile in release mode but with optimizations removed (option /Od) and see what happens....
RoadWarrior
Posts: 73
Joined: Fri Jan 13, 2012 12:39 am
Location: London, England

Re: Newbie C++ compiling question

Post by RoadWarrior »

jacobbl wrote:Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.

So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.

Regards
Jacob
In VB and C#, there is a compile-time compilation to IL bytecode, followed by a run-time (JIT) compilation to machine code. The latter is where nearly all of the optimisation happens for a release build. The JIT compiler is significantly more constrained than the C++ compiler, as it has to execute very fast. So the JIT compiler does significantly fewer optimisations when compared to the C++ compiler.

I would start by compiling your release build with all optimisations removed. Assuming that works, tweak each of the C++ optimisation parameters one by one, experimenting to see the effect on your executable. At some point you will probably find the parameter that's causing your problem. It's likely that the compiler optimisation is making an assumption about your code that isn't true.
There are two types of people in the world: Avoid them both.
User avatar
Codesquid
Posts: 138
Joined: Tue Aug 23, 2011 10:25 pm
Location: Germany

Re: Newbie C++ compiling question

Post by Codesquid »

If things work in Debug but not in Release or vice-versa, a common cause is uninitialized variables.
nanos gigantium humeris insidentes
jacobbl
Posts: 80
Joined: Wed Feb 17, 2010 3:57 pm

Re: Newbie C++ compiling question

Post by jacobbl »

Thanks to everyone for helpful answers. When I turn off Stack Frames (/RTCs) it seems to work fine. Only problem is that node count falls from 1.7M nps to 0.76M nps :-(

Does anyone have an idea of what sort of error I made that is dependent on the Stack Frames?

Regards
Jacob
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Newbie C++ compiling question

Post by Joost Buijs »

Basically /RTCs is initializing local variables to zero and checking local array bounds. Your problem is possibly related to an uninitialized local variable.
jacobbl
Posts: 80
Joined: Wed Feb 17, 2010 3:57 pm

Re: Newbie C++ compiling question

Post by jacobbl »

Thaks for the tip. So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds. I could go through all local variables and initilize them. Is this a good idea? I also use som local arrays (char lVar[90]), could this pose a problem if not initilized? I don't think I use any values before they are given a value.

Regards Jacob
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Newbie C++ compiling question

Post by mcostalba »

jacobbl wrote:Thaks for the tip. So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds.
In case of using an uninitilized local variable compiler should warn you about this (gcc for sure and probably also VC Express with warning level set to 3), in case of out of bound access in 999 out of 1000 of cases you end up with a crash.
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Newbie C++ compiling question

Post by Joost Buijs »

jacobbl wrote:So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds. I could go through all local variables and initilize them. Is this a good idea? I also use som local arrays (char lVar[90]), could this pose a problem if not initilized? I don't think I use any values before they are given a value.
You have to make sure that your local variables and arrays are initialized before you actually use their contents. VC normally gives you a warning when you use uninitialized variables.

Another possibility is that you encounter a stack-overflow. I don't know how large your local data structures are. VC defaults to 1 MB stack-size.
It's possible that the stack grows automatically when you specify /RTCs, but I'm not sure about this.