Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable GCC "may be used uninitialized" on a particular variable

I'm getting this warning on a stack variable:

warning: object.member may be used uninitialized in this function 

In this case I do not wish to force initialization to just to get rid of the warning as it consumes CPU cycles. The variable is a POD structure so a memset on it is not zero cost. I can verify that the variable is never used uninitialized, so I'd just like to suppress the warning for it.

In general I do want the warning, just not on this particular variable in this particular scenario. How can I suppress the warning?


Looks like the pragma diagnostics are the correct way to go but they require quite a recent version of GCC (4.6)

No acceptable solution prior that version is known.

like image 386
edA-qa mort-ora-y Avatar asked Feb 22 '11 16:02

edA-qa mort-ora-y


People also ask

Why are uninitialized variables bad?

"Uninitialized variables contain some value" is a incorrect statement which unfortunately is teached. A program who access an uninitialized variable has Undefined Behavior, which means it can have any behavior.

What is the default value of an uninitialized variable in C?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is assigned a memory location by the compiler, the default value of that variable is whatever (garbage) value happens to already be in that memory location!

Which is the value of uninitialized the variable?

INTRODUCTION: An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using.

What is the value of uninitialized int in C?

The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea.


1 Answers

Try doing this:

 #pragma GCC diagnostic ignored "-Wuninitialized"         foo(b);         /* no diagnostic for this one */ 

This pragma comes in three interesting and helpful flavors : warning, error, ignored. See 6.56.10 Diagnostic Pragmas for their usages. The link says,

GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project's policy might require that all sources compile with -Werror but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

like image 166
Nawaz Avatar answered Sep 22 '22 10:09

Nawaz