Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: Evaluation - 05.12

>
>
>
The compiler is to blame for everything

The compiler is to blame for everything

Sep 10 2012
Author:

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 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.



Comments (0)

Next comments next comments
close comment form