-1

When compiling in GCC with -Wall, GCC will warn about using variables that might be uninitialized. However, as the programmer, I know that a variable must be initialized if control enters a certain if statement:

int foo;

/* conditional assignment of foo goes here */

if (/* complicated condition that guarantees that foo has been initialized */){
    return test(foo);
}

In the above code, GCC will complain that foo may be used uninitialized. In particular, the complain will arise from inside the definition of test(), where foo is actually used.

Is there a GCC builtin or equivalent to tell the compiler that foo is already initialized? For example:

if (/* complicated condition that guarantees that foo has been initialized */){
    __assume_initialized(foo);
    return test(foo);
}

Note that placing #pragma GCC diagnostic ignored "-Wuninitialized" before the line return test(foo) is insufficient as the uninitialized warning arises from the usage (e.g. comparison) of foo in the definition of test(), possibly in another compilation unit, where the pragma won't exist.

Neither do I want to put the pragma inside the definition of test(), because the issue is not with test(). test() should of course not be called with random garbage, and putting the pragma there might cause other mistakes to go unnoticed.

Any solutions?

Bernard
  • 5,209
  • 1
  • 34
  • 64
  • If you know it will always be initialized in a conditional, remove the conditional? – juanchopanza Jun 06 '17 at 06:23
  • Do you have a more complicated type than `int`? If not, can you just initialise it with some constant instead? – viraptor Jun 06 '17 at 06:24
  • @viraptor It's actually a `class`, so I'd prefer not to do that. – Bernard Jun 06 '17 at 06:27
  • @Bernard: that should go into your question. – Basile Starynkevitch Jun 06 '17 at 06:28
  • @juanchopanza I don't understand what you mean. The conditional is required because the return value should only be `test(foo)` under the circumstances where the conditional evaluates to true. Otherwise, some other value is returned. – Bernard Jun 06 '17 at 06:29
  • Looks like an overenginnering concern and perhaps some [XY problem](http://xyproblem.info). Why can't you initialize that variable? What is the actual code doing? How large is the class of `foo` ? Did you benchmark that the initialization is harmful to performance? – Basile Starynkevitch Jun 06 '17 at 06:31
  • @BasileStarynkevitch Yeah, perhaps it is overengineering. The overhead is very small, and not noticeable to performance. – Bernard Jun 06 '17 at 06:33
  • 1
    If the type is a class, then the compiler won't give you the warning. Constructor would be called. If the pointer is being used, just initialize with null, or use `unique_ptr` – Ajay Jun 06 '17 at 06:37
  • 1
    _The complain will arise from inside the definition of `test()`_. Then it can't have anything to do with your `foo`. `test()` will only see the passed value, and not how it is (possibly uninitialized) passed. What is the exact case you have, and warning message? – king_nak Jun 06 '17 at 06:42

1 Answers1

2

The simplest solution is to (redundantly) initialize that variable e.g. by adding:

int foo=0;

the optimizing compiler (e.g. g++ -O2) is very likely to remove that initialization if it is unneeded. BTW, if the compiler don't optimize that in your particular case, the overhead of such an initialization is really small.

If foo is an instance of some class you could define a no-op constructor (perhaps a private: one) and make your function friend of that class. If foo is some pointer initialize it to nullptr.

BTW, such an explicit initialization makes your code more readable and less brittle and perhaps even more CPU cache friendly. You might add a comment explaining why and how it might be unneeded. IMHO the readability increase is enough to justify such an initialization.

I tend to always initialize locals, leaving the compiler the opportunity to optimize away most of the useless initializations.

Alternatively (but I don't recommend that) use a function specific pragma (as mentioned in your question) to disable such a warning.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547