Warnings during compilation

Hi all.

Marvellously, a large group of programmers believe in this golden rule of compilation: "Errors - Oh, No! Warnings - I Don’t care!"

Please! THIS IS NOT TRUE. WARNINGS ARE INDICATORS OF LURKING PROBLEMS, THAT CAN (AND USUALLY WILL) LEAD TO REAL PROBLEMS LATER!!!

A program that compiles without errors, but gives a few warnings need not necessarily run successfully. Runtime errors (like, segmentation fault (SIGSEGV)), wrong answers or “unexplainable behaviours” can all happen, because of some reason mentioned in a warning!

The warnings are there for a reason. Please read and understand them properly, and try to remove them from your code (of course, understand, why the warning came in the first palce, and then make necessary changes). I would encourage you to enable all warnings during compilation, when you are compiling your program locally. In C/C++, this can be done by adding the compiler option -Wall during the compilation step.

For example: gcc prog.c -o prog -Wall

This will display all warnings, which might have been hidden by default. Armed with this, make sure that your program has no compile time warnings as well, before executing your program.

PS: Of course, this does not guarantee, that a program that compiles without errors and warnings, will not throw a runtime error. Only, the number of instances of that happening will be reduced.


In C/C++, the simplest and most innocent warning message that anyone can come across is

``` format '%d' expects type 'int *', but argument 2 has type 'int' ```

This comes when you forget to put an `&` in your `scanf`. This will almost surely throw a SIGSEGV.

Try changing scanf("%d", t); into scanf("%d", &t);

To share an experience, I have a friend, who almost failed his lab exams, because of forgetting a single &. He got through with very low marks simply based on the design he submitted. You can imagine the frustration, when he identified who/what the culprit was.


Many a times, trying to remove warnings from someone’s code have helped me to recover their code from runtime errors. In a few occasions, I could also correct some wrong answers as well.

I am posting this here, in the hope that at least a few of you shall also benefit from my experiences.

Happy Coding!

Best!

Tijo

3 Likes

Hello Tijo,

Thanks for sharing this… :slight_smile: Actually using -Wall flag which I believe to stand for Warning All (as in, to display ALL warnings) can be a good way to ensure that the code that it’s written is completely warning free, which is, of course, very desirable!

However, for people working with C/C++ (most especially C) the warning of scanf() behaviour you mentioned might be an indicator that those people don’t really understand how memory is managed and how the scanf() function works “under the hood”.

On that specific note, scanf() expects two arguments, namely a format descriptor and a pointer to a given type, on this case, an integer.

The usage of the ampersand symbol there, as argument, is used to indicate that we are dereferencing the pointer and that we will actually read from standard input the value we want to place on the memory location pointed by our variable.

This is quite a complication only to handle input, but, solely trying to use -Wall flag blindly to fix warnings (I assume, with the best intentions of course) can actually be quite dangerous, especially when such warnings and possibly future runtime errors or compile errors, require some understanding of how memory is managed and handled internally, which is something that SHOULD be in theory fully understood by programmers who intend to compete at algorithmic level :smiley:

Nontheless, kudos for putting this up here as it might serve as a good incentive for some people to delve deeper into the wonderful world of C/C++.

Also, I believe cplusplus website to be the BEST reference online to learn C/C++ in’s and out’s.

Best regards,

Bruno

2 Likes

Yep. You are right. Relying solely on this will also not do.

I just wanted to help, by saying that “do not take warnings lightly”.

By “removing the warnings”, I of course meant, “to understand why the warning came there in the first place, and then remove it”. Not just blindly removing it.

yes, I also meant it because most people just ignore warnings completely… Like that joke of a programmer smoking and his wife saying: “Don’t you know how to read the warnings on the pack, that its bad for you and can harm you?” To which he replies: “I am a programmer, I only care about errors, not warnings” :smiley:

2 Likes

And yes, -Wall stands for display ALL Warnings.

I personaly use CFLAGS=-Wall -Werror -pedantic. That way, my code won’t compile until there is no warning left. I do that to prevent myself from being stuck in kind of situations you very well described.

1 Like

Treating warnings as errors. Got to give this a try.

Better, not to create an executable, than to make one that might crash/fail!! :slight_smile:

1 Like

my exact point of view :slight_smile:

1 Like

Take a look at this:

Problem: Transform the Expression

Solution 1 (RE): http://www.codechef.com/viewplaintext/2389098

Solution 2 (AC): http://www.codechef.com/viewplaintext/2398880

The only difference between the AC solution and the RE solution is that the data-type of variable t is changed from char in Solution 1 to int in Solution 2.

A simple and innocent change that was, and no one would ever notice. But that silent killer was mentioned, in a warning message format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’