Why does this code trigger gcc's “maybe uninitialized” warning?


Why does this code trigger gcc's “maybe uninitialized” warning?



In plenty of other instances, gcc seems to be able to detect that the condition at the beginning of the loop initializes the variable. It even works if I drop the increment operator. It also goes away without the -02 flag. I've learned to trust that compiler warnings usually do mean something is wrong, so I'm wondering if there's something I'm missing or if it's just a weird compiler quirk.


-02


void act(char **);

void test(int width, int height){
char * rowA[width];
char ** A;

for (int i = 0; i < width * height; ++i){
if (!(i % width)){
if (i){
act(rowA);
}
rowA[0] = 0;
A = rowA;
}
*A++ = "hello";
}
}



Compiling on gcc-6.3.0 with -Wall -Werror -02


gcc-6.3.0


-Wall -Werror -02



Edit: to avoid confusion I've altered the code to be closer to the actual use case.





Is !(i % argc) something you changed i == 0 to to trigger this warning? (Also, can you try gcc 8.1.1?)
– Ry-
Jun 29 at 2:23



!(i % argc)


i == 0





@Ry- in the real code I was working on it was i < width * height and i % width. Can't find a good way to try 8.1.1, but 8.1.0 still shows the warning.
– Violet
Jun 29 at 2:39


i < width * height


i % width




1 Answer
1



The warning means that the compiler couldn't prove the variable was always initialized before use.



This particular warning cannot be perfectly accurate of course, and it errs on the side of caution in that it does raise the warning if it wasn't sure. When using this warning it is common to get this sort of situation where you have to modify your correct code to make the warning go away.



Possibly it is also evidence of an optimizer bug; the optimizer should realize that if(!(i % argc)) can be optimized to if (i == 0).


if(!(i % argc))


if (i == 0)





in the real code I was working on it was i < width * height and i % width so that wouldn't've been optimizable, but you're right my simplification should've been.
– Violet
Jun 29 at 2:31


i < width * height


i % width






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV