Using volatile qualifier suppresses compiler warning
Today I was reviewing a guys's code where he had declared a variable volatile. On asking about it, he told it produces weird behaviour on some systems.
On removing volatile and compiling, it was producing this compiler warning
iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
The code was very much similar to the below code, array being accessed out of bound.
Since he was using a different codebase where Makefile was different, this warning was not produced on his system.
int a[4]={1,2,3,4};
int i; //when declared volatile int i, doesn't produce warning
i=0;
while(i<5) {
printf("%dt", a[i]); //a[4] will invoke undefined behavior
i+=2;
}
Now, I'm unable to figure out two things:
- Which exact gcc flags should I enable to get this warning?
- Why declaring
ias volatile is suppressing that warning?
c volatile
add a comment |
Today I was reviewing a guys's code where he had declared a variable volatile. On asking about it, he told it produces weird behaviour on some systems.
On removing volatile and compiling, it was producing this compiler warning
iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
The code was very much similar to the below code, array being accessed out of bound.
Since he was using a different codebase where Makefile was different, this warning was not produced on his system.
int a[4]={1,2,3,4};
int i; //when declared volatile int i, doesn't produce warning
i=0;
while(i<5) {
printf("%dt", a[i]); //a[4] will invoke undefined behavior
i+=2;
}
Now, I'm unable to figure out two things:
- Which exact gcc flags should I enable to get this warning?
- Why declaring
ias volatile is suppressing that warning?
c volatile
add a comment |
Today I was reviewing a guys's code where he had declared a variable volatile. On asking about it, he told it produces weird behaviour on some systems.
On removing volatile and compiling, it was producing this compiler warning
iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
The code was very much similar to the below code, array being accessed out of bound.
Since he was using a different codebase where Makefile was different, this warning was not produced on his system.
int a[4]={1,2,3,4};
int i; //when declared volatile int i, doesn't produce warning
i=0;
while(i<5) {
printf("%dt", a[i]); //a[4] will invoke undefined behavior
i+=2;
}
Now, I'm unable to figure out two things:
- Which exact gcc flags should I enable to get this warning?
- Why declaring
ias volatile is suppressing that warning?
c volatile
Today I was reviewing a guys's code where he had declared a variable volatile. On asking about it, he told it produces weird behaviour on some systems.
On removing volatile and compiling, it was producing this compiler warning
iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
The code was very much similar to the below code, array being accessed out of bound.
Since he was using a different codebase where Makefile was different, this warning was not produced on his system.
int a[4]={1,2,3,4};
int i; //when declared volatile int i, doesn't produce warning
i=0;
while(i<5) {
printf("%dt", a[i]); //a[4] will invoke undefined behavior
i+=2;
}
Now, I'm unable to figure out two things:
- Which exact gcc flags should I enable to get this warning?
- Why declaring
ias volatile is suppressing that warning?
c volatile
c volatile
edited Nov 16 '18 at 19:13
melpomene
58.7k54489
58.7k54489
asked Nov 16 '18 at 18:59
Raman
1,5701332
1,5701332
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
When aggressive loop optimization sees the following code...
int i;
i=0;
while(i<5) {
printf("%dt", a[i]);
i+=2;
}
... it's going to use a technique called "loop unrolling" to rewrite it like this...
printf("%dt", a[0]);
printf("%dt", a[2]);
printf("%dt", a[4]);
Problem! Iterations 0 and 1 are fine, but iteration 2 will perform an out-of-bounds array access, invoking undefined behavior. That's why you got the warning you did.
Declaring i to be volatile prevents the compiler from doing this optimization (because it can't be sure that another process isn't modifying the value of i during loop execution), so it has to leave the code as it was. You still have the undefined behavior, it's just that the compiler doesn't warn you about it. All in all, a terrible "fix" from your co-worker.
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
add a comment |
The undefined behavior you have is that your loop allows i=4, which will read past the end of the array. This is noticed by the loop optimizer, but of course it's a problem regardless of optimization.
volatile tells the compiler that the value of i can be changed from outside this code. The practical effect of this is that the compiler cannot do optimizations that depend on assuming the value of i. That turns off the optimization that noticed your problem.
Using volatile solely to bypass the warning is terrible practice. Instead change the while condition to while (i < 4).
add a comment |
- The warning tells you which flag enables it:
-Waggressive-loop-optimizations
- Declaring a variable
volatilemeans the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume thatiwill ever take the value4(or thati += 2will always add 2 toi's value, etc).
-Waggressive-loop-optimizationswas the first thing I tried, no luck
– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like-Oor-O2?
– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with-Osolves the problem. Thank you.
– Raman
Nov 16 '18 at 19:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343882%2fusing-volatile-qualifier-suppresses-compiler-warning%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When aggressive loop optimization sees the following code...
int i;
i=0;
while(i<5) {
printf("%dt", a[i]);
i+=2;
}
... it's going to use a technique called "loop unrolling" to rewrite it like this...
printf("%dt", a[0]);
printf("%dt", a[2]);
printf("%dt", a[4]);
Problem! Iterations 0 and 1 are fine, but iteration 2 will perform an out-of-bounds array access, invoking undefined behavior. That's why you got the warning you did.
Declaring i to be volatile prevents the compiler from doing this optimization (because it can't be sure that another process isn't modifying the value of i during loop execution), so it has to leave the code as it was. You still have the undefined behavior, it's just that the compiler doesn't warn you about it. All in all, a terrible "fix" from your co-worker.
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
add a comment |
When aggressive loop optimization sees the following code...
int i;
i=0;
while(i<5) {
printf("%dt", a[i]);
i+=2;
}
... it's going to use a technique called "loop unrolling" to rewrite it like this...
printf("%dt", a[0]);
printf("%dt", a[2]);
printf("%dt", a[4]);
Problem! Iterations 0 and 1 are fine, but iteration 2 will perform an out-of-bounds array access, invoking undefined behavior. That's why you got the warning you did.
Declaring i to be volatile prevents the compiler from doing this optimization (because it can't be sure that another process isn't modifying the value of i during loop execution), so it has to leave the code as it was. You still have the undefined behavior, it's just that the compiler doesn't warn you about it. All in all, a terrible "fix" from your co-worker.
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
add a comment |
When aggressive loop optimization sees the following code...
int i;
i=0;
while(i<5) {
printf("%dt", a[i]);
i+=2;
}
... it's going to use a technique called "loop unrolling" to rewrite it like this...
printf("%dt", a[0]);
printf("%dt", a[2]);
printf("%dt", a[4]);
Problem! Iterations 0 and 1 are fine, but iteration 2 will perform an out-of-bounds array access, invoking undefined behavior. That's why you got the warning you did.
Declaring i to be volatile prevents the compiler from doing this optimization (because it can't be sure that another process isn't modifying the value of i during loop execution), so it has to leave the code as it was. You still have the undefined behavior, it's just that the compiler doesn't warn you about it. All in all, a terrible "fix" from your co-worker.
When aggressive loop optimization sees the following code...
int i;
i=0;
while(i<5) {
printf("%dt", a[i]);
i+=2;
}
... it's going to use a technique called "loop unrolling" to rewrite it like this...
printf("%dt", a[0]);
printf("%dt", a[2]);
printf("%dt", a[4]);
Problem! Iterations 0 and 1 are fine, but iteration 2 will perform an out-of-bounds array access, invoking undefined behavior. That's why you got the warning you did.
Declaring i to be volatile prevents the compiler from doing this optimization (because it can't be sure that another process isn't modifying the value of i during loop execution), so it has to leave the code as it was. You still have the undefined behavior, it's just that the compiler doesn't warn you about it. All in all, a terrible "fix" from your co-worker.
answered Nov 16 '18 at 19:33
Tim Randall
1,9221119
1,9221119
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
add a comment |
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
Nicely explained. Thanks
– Raman
Nov 16 '18 at 19:55
add a comment |
The undefined behavior you have is that your loop allows i=4, which will read past the end of the array. This is noticed by the loop optimizer, but of course it's a problem regardless of optimization.
volatile tells the compiler that the value of i can be changed from outside this code. The practical effect of this is that the compiler cannot do optimizations that depend on assuming the value of i. That turns off the optimization that noticed your problem.
Using volatile solely to bypass the warning is terrible practice. Instead change the while condition to while (i < 4).
add a comment |
The undefined behavior you have is that your loop allows i=4, which will read past the end of the array. This is noticed by the loop optimizer, but of course it's a problem regardless of optimization.
volatile tells the compiler that the value of i can be changed from outside this code. The practical effect of this is that the compiler cannot do optimizations that depend on assuming the value of i. That turns off the optimization that noticed your problem.
Using volatile solely to bypass the warning is terrible practice. Instead change the while condition to while (i < 4).
add a comment |
The undefined behavior you have is that your loop allows i=4, which will read past the end of the array. This is noticed by the loop optimizer, but of course it's a problem regardless of optimization.
volatile tells the compiler that the value of i can be changed from outside this code. The practical effect of this is that the compiler cannot do optimizations that depend on assuming the value of i. That turns off the optimization that noticed your problem.
Using volatile solely to bypass the warning is terrible practice. Instead change the while condition to while (i < 4).
The undefined behavior you have is that your loop allows i=4, which will read past the end of the array. This is noticed by the loop optimizer, but of course it's a problem regardless of optimization.
volatile tells the compiler that the value of i can be changed from outside this code. The practical effect of this is that the compiler cannot do optimizations that depend on assuming the value of i. That turns off the optimization that noticed your problem.
Using volatile solely to bypass the warning is terrible practice. Instead change the while condition to while (i < 4).
answered Nov 16 '18 at 19:19
Adam
12.6k33879
12.6k33879
add a comment |
add a comment |
- The warning tells you which flag enables it:
-Waggressive-loop-optimizations
- Declaring a variable
volatilemeans the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume thatiwill ever take the value4(or thati += 2will always add 2 toi's value, etc).
-Waggressive-loop-optimizationswas the first thing I tried, no luck
– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like-Oor-O2?
– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with-Osolves the problem. Thank you.
– Raman
Nov 16 '18 at 19:53
add a comment |
- The warning tells you which flag enables it:
-Waggressive-loop-optimizations
- Declaring a variable
volatilemeans the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume thatiwill ever take the value4(or thati += 2will always add 2 toi's value, etc).
-Waggressive-loop-optimizationswas the first thing I tried, no luck
– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like-Oor-O2?
– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with-Osolves the problem. Thank you.
– Raman
Nov 16 '18 at 19:53
add a comment |
- The warning tells you which flag enables it:
-Waggressive-loop-optimizations
- Declaring a variable
volatilemeans the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume thatiwill ever take the value4(or thati += 2will always add 2 toi's value, etc).
- The warning tells you which flag enables it:
-Waggressive-loop-optimizations
- Declaring a variable
volatilemeans the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume thatiwill ever take the value4(or thati += 2will always add 2 toi's value, etc).
answered Nov 16 '18 at 19:13
melpomene
58.7k54489
58.7k54489
-Waggressive-loop-optimizationswas the first thing I tried, no luck
– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like-Oor-O2?
– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with-Osolves the problem. Thank you.
– Raman
Nov 16 '18 at 19:53
add a comment |
-Waggressive-loop-optimizationswas the first thing I tried, no luck
– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like-Oor-O2?
– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with-Osolves the problem. Thank you.
– Raman
Nov 16 '18 at 19:53
-Waggressive-loop-optimizations was the first thing I tried, no luck– Raman
Nov 16 '18 at 19:19
-Waggressive-loop-optimizations was the first thing I tried, no luck– Raman
Nov 16 '18 at 19:19
@Raman Did you also enable optimizations in general? I.e. something like
-O or -O2?– melpomene
Nov 16 '18 at 19:25
@Raman Did you also enable optimizations in general? I.e. something like
-O or -O2?– melpomene
Nov 16 '18 at 19:25
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Yes, tried those. But seems like problem is about gcc version.
– Raman
Nov 16 '18 at 19:28
Using latest version of gcc with
-O solves the problem. Thank you.– Raman
Nov 16 '18 at 19:53
Using latest version of gcc with
-O solves the problem. Thank you.– Raman
Nov 16 '18 at 19:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343882%2fusing-volatile-qualifier-suppresses-compiler-warning%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown