User account creation filtered due to spam.

Bug 48801

Summary: uninitialized warning for variables used only in loop body and initialized in the first iteration
Product: gcc Reporter: Ángel <Kip.Hicit>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Severity: minor CC: law
Priority: P3    
Version: 4.6.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-04-28 10:16:52
Bug Depends on:    
Bug Blocks: 24639    

Description Ángel 2011-04-28 08:56:13 UTC
Consider the code:

int loop_cond, some_value;

void foo(int arg);
void f() {
     int load, variable;
     load = 1;
     while (loop_cond) {
       if (load) {
          variable = some_value;
          load = 0;


gcc -Wall -O1 initialise-warn.c
initialise-warn.c: In function `f':
initialise-warn.c:5: warning: 'variable' might be used uninitialized in this fun

variable is only used inside the loop, and always set at the beginning of its first iteration.
The warning is gone if using a conditional instead of a loop, so it seems that it could easily detect that it is initialised in this case, too. Perhaps it was optimizing the constant with an if, but the load variable is local and can only be changed inside the function, so it has no extra restrictions.

The warning appears in gcc 4.5.2 and 4.6.0
The false positive was probably always misreported.
Comment 1 Richard Biener 2011-04-28 10:16:52 UTC
Comment 2 Jeffrey A. Law 2013-11-20 01:10:11 UTC
gcc-4.8 and the trunk seem to handle this just fine.