Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Warnings vs optimization gcc

I wonder if the warnings reported by compiler such as variable unused or control reaches end of a non-void function can impact a program (i.e crash) when the optimization is enabled (O2 or O3)

Could you please give me some examples ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Warnings indicate likely mistakes in your code. However, whether or not the warning is there or whether or not optimizations are enabled doesn’t affect whether the code is correct.

Warnings such as unused variable simply indicate that you probably meant to use the variable somewhere but forgot to do so. Otherwise there wouldn’t be a reason for the variable to be there.

Warnings such as control reaches end of a non-void function are more severe. For example in this specific case, calling a function with non-void return type causes undefined behavior if its execution reaches the function body’s closing } without returning via a return statement prior to that.

In this case the warning is informing you that the compiler detected a path that the function could take for some input with this result. This is very likely unintended, because if you call that function taking that path, then your program would have undefined behavior.


When the program has undefined behavior you cannot be sure what it will do, absent additional guarantees made by your compiler/platform. It may behave in one way with optimizations and in a different way without them. It might also have different behavior with different compiler versions or just between different compilation runs or even program executions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading