The compiler is to blame for everything

Many programmers are very fond of blaming the compiler for different errors. Let's talk about it.

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 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)
  if (!init && IsClip() && clip)
  // make sure this copies the whole struct!
  //((__int32*)this)[0] = ((__int32*)src)[0];
  //((__int32*)this)[1] = ((__int32*)src)[1];

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.


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


By the way, not so long ago I gave a speech about this and other interesting searches on PHDays Forum 2015. I hope it would be useful and fun. Here it is my report with simultaneous translation from Russian into English: . The report's start: 06:13:21. You can easily find it by number 200 on the page and choosing this report in the list.

Also, I uploaded the presentation itself in English:

For more complete information about compiler optimizations, see our Optimization Notice.


Dan S.'s picture

Hmmm... I've had bugs where the compiler genuinely caused the problem. Then again, I wrote the compiler...!

Andrey Karpov's picture

Yes, there is a problem that progammers spoil vptr. By the way, there is a special diagnistics of V598 in PVS-Studio, for searching this error type. There are some samples of errors:

Also, some programmers suppose that if they write __declspec(novtable) they will handle with this problem. But in reality there is nothing so easy. We wrote an article about it: Virtual Method Table and accident prevention.

Mario R.'s picture

I'm with Paulo here. While it is an obvious mistake to memset to the size of a pointer (unless your data actually is a pointer, but why wouldn't you just assign it then?), I think it's actually worse that the programmer actually considers memsetting a class as a good option. Color note, with the right flags (I'm not sure if it's default, or just one of the flags enabled by Wextra, Wall or Weffc++), clang warns you when you memset an abstract class :) Haven't seen this happen with gcc, though.

Sebastian R.'s picture

Of course there is a bug by the programmer, but the compiler could be more helpful, for example by printing a useful error message:

warning: 'memset' call operates on objects of type 'longstruct' while the size is based on a different type 'longstruct *' [-Wsizeof-pointer-memaccess]
note: did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?

Paulo Z.'s picture

When I saw the memset I imagined the problem was the fact that the V-Table pointer was being cleared (it is contained in all classes that have at least one virtual method).

I didn't notice the sizeof(*this) problem... yet, I wouldn't use the memset to initialize a type, even with the right size, because of the risk of clearing some internal content (like the vTable) that's compiler specific.

Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.