|
|
|
|
|
by pierrebai
4478 days ago
|
|
If you're using var x = x; then stop. The compiler is able to validate that assignment to variables are never used using data flow analysis, so always initializing has no cost once optimizations are turned on. Beside, even if it was not optimized away, until a profiler actualy shows that a variable initialization is your bottleneck, it's a waste of time and will make code refactoring harder and cuase bug down the line. Maybe not in this function, maybe not by you, but someone will add a new conditional branch somewhere where your variable won't be initialized. As for the optimization, I just tested on my machine: int main(int argc, char** argv)
{
int x = 0;
switch (argc)
{
case 0:
x = 1;
break;
case 1:
x = 5;
break;
case 2:
x = 7;
break;
default:
x = 9;
break;
}
return x;
}
gcc -O3 -o opt-assign opt-assign.c
(gdb) disassemble main
Showed that x is never assigned zero. |
|