Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
19 Views

bad loop optimization

[cpp]

void foo(int a) {
    while(a);
}

int main() {
    foo(1);
}[/cpp]

Compiler removes empty loop and there is no endless loop that i expect. MSVC and GCC works correctly.

Windows, ICC 13.1.

0 Kudos
5 Replies
Highlighted
Valued Contributor II
19 Views

With Intel C++ compiler this is a well know issue but it is Not a problem. What optimization options did you use, /O1, /O2 or /O3?
0 Kudos
Highlighted
Beginner
19 Views

With any level of optimization.

0 Kudos
Highlighted
Valued Contributor II
19 Views

>>...Compiler removes empty loop and there is no endless loop that i expect... Do you need to use a similar processing in a real application?
0 Kudos
Highlighted
Moderator
19 Views

Adding "#pragma intel optimization_level 0" before the function "foo()" will keep the loop. 

Or introduce a volatile variable like below: 

//#pragma intel optimization_level 0
void foo(int a) {
    volatile int b=a;
    while(a)
    ;
}

0 Kudos
Highlighted
Beginner
19 Views

Okay, i forgot about volatile, thanks.

0 Kudos