volatile const

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

    volatile const

    Hi Everyone,

    The following code compiles without any error,

    int main()
    {
    volatile const int a = 10;
    return(0);
    };

    But i expected a compilation error as a is being told as a constant
    and at the same time indicating that its value could change outside
    the program's control...

    Why isn't this an error?

    Thanks in advance ! ! !
  • Andrey Tarasevich

    #2
    Re: volatile const

    Rahul wrote:
    The following code compiles without any error,
    >
    int main()
    {
    volatile const int a = 10;
    return(0);
    };
    >
    But i expected a compilation error as a is being told as a constant
    and at the same time indicating that its value could change outside
    the program's control...
    >
    Why isn't this an error?
    Why should it be an error? This is exactly what 'volatile const' is
    supposed to mean in the first place: the program is not allowed to
    change the object, but its value can change "by itself".

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Richard Bos

      #3
      Re: volatile const

      Rahul <sam_cit@yahoo. co.inwrote:
      int main()
      {
      volatile const int a = 10;
      return(0);
      };
      >
      But i expected a compilation error as a is being told as a constant
      No, it's being declared as something _you_ won't change.
      and at the same time indicating that its value could change outside
      the program's control...
      Quite. It is something your code won't change, but something else will.
      One example of this could be a hardware counter, or an input port.

      Richard

      Comment

      • Joachim Schmitz

        #4
        Re: volatile const

        Rahul wrote:
        Hi Everyone,
        >
        The following code compiles without any error,
        >
        int main()
        {
        volatile const int a = 10;
        return(0);
        };
        >
        But i expected a compilation error as a is being told as a constant
        and at the same time indicating that its value could change outside
        the program's control...
        >
        Why isn't this an error?
        Because for some stupid reason in C const doesn't mean 'constant' but 'read
        only'

        Bye,Jojo


        Comment

        • Richard Tobin

          #5
          Re: volatile const

          In article <ftkmd2$v35$1@a ioe.org>,
          Andrey Tarasevich <andreytarasevi ch@hotmail.comw rote:
          > volatile const int a = 10;
          >Why should it be an error? This is exactly what 'volatile const' is
          >supposed to mean in the first place: the program is not allowed to
          >change the object, but its value can change "by itself".
          The standard gives this example:

          extern const volatile int real_time_clock ;

          You can't change the time, but it changes anyway.

          -- Richard
          --
          :wq

          Comment

          • raof01

            #6
            Re: volatile const

            Hello Joachim,
            Because for some stupid reason in C const doesn't mean 'constant' but
            'read only'
            >
            Yes, absolutely. Furthermore, "const" means read only JUST for you. On the
            other hand, "volatile" doesn't mean you can modify it but you HAVE TO reload
            it from memory every time you use it. "volatile" is more like an indicator
            for compiler to protect from optimizing.

            raof01
            "Thoughts are but dreams till their effects be tried. -- William Shakespeare"


            Comment

            • Chris Thomasson

              #7
              Re: volatile const

              "raof01" <raof01@gmail.c omwrote in message
              news:7c6e25954a 388ca6bb36481ec c0@news.cn99.co m...
              Hello Joachim,
              >
              >Because for some stupid reason in C const doesn't mean 'constant' but
              >'read only'
              >>
              >
              Yes, absolutely. Furthermore, "const" means read only JUST for you. On the
              other hand, "volatile" doesn't mean you can modify it but you HAVE TO
              reload it from memory every time you use it. "volatile" is more like an
              indicator for compiler to protect from optimizing.
              Its not clear at what level a compiler will stop optimizing; its all
              implementation defined. It seems like link-time optimizations can mess
              around with volatiles if they can obtain certain levels of information... I
              think smart-a$@ compilers think they can reorder something, and ultimately
              end up generating phantom behavior under the random nature of various
              race-conditions. This is especially relevant when the 'volatile' keyword is
              used in conjunction with multi-threaded algorithms...

              Comment

              Working...