Using volatile qualifier suppresses compiler warning












2














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:




  1. Which exact gcc flags should I enable to get this warning?

  2. Why declaring i as volatile is suppressing that warning?










share|improve this question





























    2














    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:




    1. Which exact gcc flags should I enable to get this warning?

    2. Why declaring i as volatile is suppressing that warning?










    share|improve this question



























      2












      2








      2







      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:




      1. Which exact gcc flags should I enable to get this warning?

      2. Why declaring i as volatile is suppressing that warning?










      share|improve this question















      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:




      1. Which exact gcc flags should I enable to get this warning?

      2. Why declaring i as volatile is suppressing that warning?







      c volatile






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 19:13









      melpomene

      58.7k54489




      58.7k54489










      asked Nov 16 '18 at 18:59









      Raman

      1,5701332




      1,5701332
























          3 Answers
          3






          active

          oldest

          votes


















          3














          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.






          share|improve this answer





















          • Nicely explained. Thanks
            – Raman
            Nov 16 '18 at 19:55



















          2














          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).






          share|improve this answer





























            1















            1. The warning tells you which flag enables it: -Waggressive-loop-optimizations

            2. Declaring a variable volatile means the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume that i will ever take the value 4 (or that i += 2 will always add 2 to i's value, etc).






            share|improve this answer





















            • -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










            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            3














            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.






            share|improve this answer





















            • Nicely explained. Thanks
              – Raman
              Nov 16 '18 at 19:55
















            3














            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.






            share|improve this answer





















            • Nicely explained. Thanks
              – Raman
              Nov 16 '18 at 19:55














            3












            3








            3






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 19:33









            Tim Randall

            1,9221119




            1,9221119












            • 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




            Nicely explained. Thanks
            – Raman
            Nov 16 '18 at 19:55













            2














            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).






            share|improve this answer


























              2














              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).






              share|improve this answer
























                2












                2








                2






                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).






                share|improve this answer












                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).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 19:19









                Adam

                12.6k33879




                12.6k33879























                    1















                    1. The warning tells you which flag enables it: -Waggressive-loop-optimizations

                    2. Declaring a variable volatile means the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume that i will ever take the value 4 (or that i += 2 will always add 2 to i's value, etc).






                    share|improve this answer





















                    • -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










                    • 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
















                    1















                    1. The warning tells you which flag enables it: -Waggressive-loop-optimizations

                    2. Declaring a variable volatile means the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume that i will ever take the value 4 (or that i += 2 will always add 2 to i's value, etc).






                    share|improve this answer





















                    • -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










                    • 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














                    1












                    1








                    1







                    1. The warning tells you which flag enables it: -Waggressive-loop-optimizations

                    2. Declaring a variable volatile means the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume that i will ever take the value 4 (or that i += 2 will always add 2 to i's value, etc).






                    share|improve this answer













                    1. The warning tells you which flag enables it: -Waggressive-loop-optimizations

                    2. Declaring a variable volatile means the compiler must assume that things outside of the compiler's control may inspect and modify the variable. Therefore it cannot assume that i will ever take the value 4 (or that i += 2 will always add 2 to i's value, etc).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 19:13









                    melpomene

                    58.7k54489




                    58.7k54489












                    • -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










                    • 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


















                    • -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










                    • 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
















                    -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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?