hii

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ganga

    #1

    hii

    Please, will anyone explain me difference between 'register' and
    'volatile' variables

  • Nick Keighley

    #2
    Re: hii

    Ganga wrote:
    [color=blue]
    > Please, will anyone explain me difference between 'register' and
    > 'volatile' variables[/color]

    is this homework? Why don't you lookup the answer in your textbook?


    --
    Nick Keighley

    Comment

    • Umesh

      #3
      Re: hii


      Ganga wrote:[color=blue]
      > Please, will anyone explain me difference between 'register' and[/color]
      Register keyword for a variable will force the compiler backend to give
      register.
      so by this compiler can acess the variable without store
      instruction(i.e it can use mov instruction).[color=blue]
      > 'volatile' variables[/color]
      Volatile Keyword for a variable will prevent variable from
      optimization.
      i.e
      main()
      {
      int i=2,j=3,a,b;
      a=i+j;
      b=i+j;

      return a+b;
      }

      For above code the compiler try to optimize the code.
      i.e
      main()
      {
      int i=2,j=3,a,b
      t=i+j; //where t is the temproray variable which is interal generated
      by compiler.
      a=t;//a & b are automatic variable.
      b=t;
      // by this we can save one addition.
      return a+b;
      }

      main()
      {
      volatile int i=2,j=3,a,b;
      a=i+j;
      b=i+j;

      return a+b;
      }

      but in the case volatile.it prevents variables(i,j) from optimization.
      so above code remain same.

      Comment

      • Umesh

        #4
        Re: hii


        Ganga wrote:[color=blue]
        > Please, will anyone explain me difference between 'register' and[/color]
        Register keyword for a variable will force the compiler backend to give
        register.
        so by this compiler can acess the variable without store
        instruction(i.e it can use mov instruction).[color=blue]
        > 'volatile' variables[/color]
        Volatile Keyword for a variable will prevent variable from
        optimization.
        i.e
        main()
        {
        int i=2,j=3,a,b;
        a=i+j;
        b=i+j;

        return a+b;
        }

        For above code the compiler try to optimize the code.
        i.e
        main()
        {
        int i=2,j=3,a,b
        t=i+j; //where t is the temproray variable which is interal generated
        by compiler.
        a=t;//a & b are automatic variable.
        b=t;
        // by this we can save one addition.
        return a+b;
        }

        main()
        {
        volatile int i=2,j=3,a,b;
        a=i+j;
        b=i+j;

        return a+b;
        }

        but in the case volatile.it prevents variables(i,j) from optimization.
        so above code remain same.

        Comment

        • Vladimir S. Oka

          #5
          Re: hii

          Umesh wrote:[color=blue]
          > Ganga wrote:[color=green]
          > > Please, will anyone explain me difference between 'register' and[/color]
          > Register keyword for a variable will force the compiler backend to give
          > register.
          > so by this compiler can acess the variable without store
          > instruction(i.e it can use mov instruction).[/color]

          Not entirely correct. The `register` keyword will /suggest/ to the
          compiler that a variable be stored in a register. Most modern compilers
          are better at optimising than programmers, and will happily ignore the
          `register` keyword.
          [color=blue][color=green]
          > > 'volatile' variables[/color]
          > Volatile Keyword for a variable will prevent variable from
          > optimization.[/color]

          Again, not entirely correct. The `volatile` keyword tells the compiler
          that the variable may be changed by something outside the scope of the
          current program (other programs in the system, hardware device, cosmic
          rays...). Because of this, the compiler cannot be certain of the value
          of the `volatile` variable, even if your code did not change it, and
          will hence avoid optimisations that rely on that value not changing
          between two points in the code.

          For example, in the snippet:

          int main(void)
          {
          volatile int i = 5;
          int j = 6;

          while (3 < i)
          {
          ++j;
          }

          return 0;
          }

          The compiler cannot assume that `i` will be 5 when it is tested for the
          `while` loop, and hence cannot replace it with the equivalent of `while
          (1)`, but has to generate the code for the test. If `i` was not
          `volatile`, the compiler could generate the equivavalent of:

          int main(void)
          {
          int j = 6;

          endless_loop:
          ++j;
          goto endless_loop;
          }

          (Note that it could get rid of `i` altogether.)

          <snipped not so good an example>

          Cheers

          Vladimir

          Comment

          • Mike Wahler

            #6
            Re: hii


            "Ganga" <ganga.jaiswal@ gmail.com> wrote in message
            news:1138695309 .636515.193410@ f14g2000cwb.goo glegroups.com.. .[color=blue]
            > Please, will anyone explain me difference between 'register' and
            > 'volatile' variables[/color]

            The difference depends upon their values.

            register int r = 42;
            volatile int v = 25;

            printf("Differe nce between %d and %d is %d\n", r, v, r - v);

            If you really mean to ask about the difference between
            the 'register' and 'volatile' keywords, the answer can
            be found in any good C book.

            -Mike


            Comment

            Working...