Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
November 22, 2014
arrowPress Releases
November 22, 2014
PR Newswire
View All
View All     Submit Event






If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
The compiler is to blame for everything
by Andrey Karpov on 09/10/12 07:05:00 am

2 comments Share on Twitter Share on Facebook    RSS

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

Are you sure?

When a programmer tells you that the compiler causes an error, it is a lie in 99% of cases. When you start investigating the problem, you usually find out the following reasons:

  • an array overrun;
  • an uninitialized variable;
  • a misprint;
  • a synchronization error in a parallel program;
  • a non-volatile variable used;
  • code leading to undefined behavior;
  • etc.

Many went through fixing such errors. Many read about them. But it doesn't prevent them from blaming the compiler for all sins again and again. Each time it seems that it's exactly it which is guilty.

The compiler, of course, might also contain errors. But this probability is very small unless you use some exotic compiler for a microcontroller. During many years of working with Visual C++ I saw only once that it had generated an incorrect assembler code.

A small recommendation

Before starting to blame the compiler and write about it in the code or on a forum, carry out a thorough investigation. First, you will eliminate an error in your code sooner. Second, you won't look silly in other programmers' eyes who will point out your slip-up.

What made me write this post

I've been much amused today by a code fragment from the ffdshow project. Here it is:

TprintPrefs::TprintPrefs(IffdshowBase *Ideci,
                         const TfontSettings *IfontSettings)
{
  memset(this, 0, sizeof(this)); // This doesn't seem to
                                 // help after optimization.
  dx = dy = 0;
  isOSD = false;
  xpos = ypos = 0;
  align = 0;
  linespacing = 0;
  sizeDx = 0;
  sizeDy = 0;
  ...
}

Looking at the comment I can imagine how angry the programmer was. Oh, that insufferable compiler! In the debug version all the variables equal 0. In the release version they contain trash because of the faulty optimization. Outrageous! Bad, bad compiler!

Having scolded the compiler, the programmer leaves an accusing comment and goes on to write a code which zeroes each class member separately. Courage conquers evil forces.

Which is worse, this person will be absolutely sure that he/she has encountered a bug in the compiler and will tell everybody how much he/she has suffered because of it.

If somebody hasn't got the humor of the situation, I will explain. The memset() function in that sample doesn't work because of a simplest error: the third argument calculates the pointer size, not the structure size. The correct call should look like this: "memset(this, 0, sizeof(*this));".

By the way, the memcpy() function nearby works poorly too. I'm sure the programmer believes compiler developers are inept creatures.

void Assign(const AVSValue* src, bool init) {
  if (src->IsClip() && src->clip)
    src->clip->AddRef();
  if (!init && IsClip() && clip)
    clip->Release();
  // make sure this copies the whole struct!
  //((__int32*)this)[0] = ((__int32*)src)[0];
  //((__int32*)this)[1] = ((__int32*)src)[1];
  memcpy(this,src,sizeof(this));
}

From the comments you can see that the programmer tried to copy memory through alternative methods. However, then he/she decided to leave the 'memcpy()' function there. Perhaps it worked well in the 64-bit program where the pointer size equals 8 bytes, while it's exactly 8 bytes that the programmer wanted to copy.

Again, there is an error in the third argument. "sizeof(*this)" should be written instead.

This is how legends about glitchy compilers and brave programmers fighting them are born.

Conclusion

If something goes wrong, search for a mistake in your code.

P.S.

How did I find these errors? Very simply - I used the PVS-Studio code analyzer.


Related Jobs

Forio
Forio — San Francisco, California, United States
[11.21.14]

Web Application Developer Team Lead
DeNA
DeNA — San Francisco, California, United States
[11.21.14]

Senior Build and Release Engineer
Filament Games LLC
Filament Games LLC — Madison, Wisconsin, United States
[11.21.14]

Game Engineer
The Workshop
The Workshop — Marina del Rey, California, United States
[11.21.14]

Programmer





Loading Comments

loader image