const volatile???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • d.f.s.

    const volatile???

    In the post below, 'copy constructor?', the answers refer to
    an object declared as const volatile. Now I'm confused.
    Are those terms not mutually exclusive?

    const='Hey compiler! This is not going to change so generate
    your code based on that assumption.'

    volatile='Hey compiler! This is going to change so generate
    your code based on that assumption.'

    Or am I out to lunch on my understanding of these two words?

    Drew


  • Victor Bazarov

    #2
    Re: const volatile???

    d.f.s. wrote:[color=blue]
    > In the post below, 'copy constructor?', the answers refer to
    > an object declared as const volatile. Now I'm confused.
    > Are those terms not mutually exclusive?[/color]

    Definitely not.
    [color=blue]
    > const='Hey compiler! This is not going to change so generate
    > your code based on that assumption.'[/color]

    No, that's not all. OOH, it could be "hey, it's not going to change",
    OTOH it could just as well be "*you* are not [allowed] to change it".
    [color=blue]
    > volatile='Hey compiler! This is going to change so generate
    > your code based on that assumption.'
    >
    > Or am I out to lunch on my understanding of these two words?[/color]

    Not completely, but you're making wrong assumptions and conclusions.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • d.f.s.

      #3
      Re: const volatile???

      [snip][color=blue][color=green]
      > > const='Hey compiler! This is not going to change so generate
      > > your code based on that assumption.'[/color]
      >
      > No, that's not all. OOH, it could be "hey, it's not going to change",
      > OTOH it could just as well be "*you* are not [allowed] to change it".[/color]

      Right. It is a promise the coder makes , which the compiler will
      enforce upon anyone who writes code to handle the object. It
      protects the object & -used to be- helps the compiler to optimize
      its' output. I think that I've got that one.
      [color=blue][color=green]
      > > volatile='Hey compiler! This is going to change so generate
      > > your code based on that assumption.'
      > >[/color][/color]
      My understanding of 'volatile':
      A key word which indicates that an object is likely to be
      or WILL be modified. Redundant for objects not
      declared const. Was used to help older compilers to
      do optimization. Sort of like 'register', it is seldom used
      or needed with modern compilers.

      I assume that my understanding of 'volatile' is wrong.

      Drew


      Comment

      • Marcus Kwok

        #4
        Re: const volatile???

        d.f.s. <none@none.ni t> wrote:[color=blue]
        > My understanding of 'volatile':
        > A key word which indicates that an object is likely to be
        > or WILL be modified. Redundant for objects not
        > declared const. Was used to help older compilers to
        > do optimization. Sort of like 'register', it is seldom used
        > or needed with modern compilers.
        >
        > I assume that my understanding of 'volatile' is wrong.[/color]

        Here's an article by Andrei Alexandrescu on the 'volatile' keyword:

        volatile - Multithreaded Programmer's Best Friend


        --
        Marcus Kwok
        Replace 'invalid' with 'net' to reply

        Comment

        • Victor Bazarov

          #5
          Re: const volatile???

          d.f.s. wrote:[color=blue]
          > [..]
          > My understanding of 'volatile':
          > A key word which indicates that an object is likely to be
          > or WILL be modified. Redundant for objects not
          > declared const.[/color]

          No, it's not.
          [color=blue]
          > Was used to help older compilers to
          > do optimization. Sort of like 'register', it is seldom used
          > or needed with modern compilers.
          >
          > I assume that my understanding of 'volatile' is wrong.[/color]

          Yes, but not entirely. 'volatile' indicates an object that can change
          by means other than the program in which it appears. It could be some
          address that a device (through a driver or DMA means) can update based
          on a change of its state (serial port receives another input, etc.)

          Declaring a variable 'volatile' makes the compiler generate code that
          reads the value from the variable's storage any time it's needed in
          an expression, precluding the compiler from caching the value or some
          other optimization WRT the variable's value.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • Selwyn Alcantara

            #6
            Re: const volatile???

            > My understanding of 'volatile':[color=blue]
            > A key word which indicates that an object is likely to be
            > or WILL be modified. Redundant for objects not
            > declared const. Was used to help older compilers to
            > do optimization. Sort of like 'register', it is seldom used
            > or needed with modern compilers.[/color]
            [color=blue]
            > I assume that my understanding of 'volatile' is wrong.[/color]

            You understood correctly (as in, it's wrong). 'volatile' isn't simple a
            cute way of saying "not const". It informs the compiler that it
            shouldn't expect to know whether the variable changes or not based on
            what it compiles. This is used when you make a variable point to an
            area of memory for example. In practice, this means that the program
            has to recheck the variable every time it's used rather than apply some
            sly optimization because it sees you yourself didn't change it in the
            code.

            Comment

            • d.f.s.

              #7
              Re: const volatile???

              Ok. So a good example would be:
              const volatile clock;

              const in that YOU are not allowed to change it.
              volatile in that it will change so look at it whenever
              CheckTime() is called.

              If right, thank you all

              Drew


              Comment

              • red floyd

                #8
                Re: const volatile???

                d.f.s. wrote:[color=blue]
                > Ok. So a good example would be:
                > const volatile clock;
                >
                > const in that YOU are not allowed to change it.
                > volatile in that it will change so look at it whenever
                > CheckTime() is called.
                >[/color]

                Exactly.

                const is "This code is not allowed to modify this variable.
                volatile is "This variable may change due to forces unrelated to this code".

                Example:

                const volatile unsigned long * const MEMORY_MAPPED_R EG =
                reinterpret_cas t<const volatile unsigned long *>(0xFFE00000L) ;

                MEMORY_MAPPED_R EG is a read only h/w register at FFE00000. It
                can't be written to, but may change due to external issues.


                Comment

                • Rolf Magnus

                  #9
                  Re: const volatile???

                  Victor Bazarov wrote:
                  [color=blue]
                  > d.f.s. wrote:[color=green]
                  >> [..]
                  >> My understanding of 'volatile':
                  >> A key word which indicates that an object is likely to be
                  >> or WILL be modified. Redundant for objects not
                  >> declared const.[/color]
                  >
                  > No, it's not.
                  >[color=green]
                  >> Was used to help older compilers to
                  >> do optimization. Sort of like 'register', it is seldom used
                  >> or needed with modern compilers.
                  >>
                  >> I assume that my understanding of 'volatile' is wrong.[/color]
                  >
                  > Yes, but not entirely. 'volatile' indicates an object that can change
                  > by means other than the program in which it appears.[/color]

                  Or that it may be read by something else than that program.
                  [color=blue]
                  > It could be some address that a device (through a driver or DMA means) can
                  > update based on a change of its state (serial port receives another input,
                  > etc.)
                  >
                  > Declaring a variable 'volatile' makes the compiler generate code that
                  > reads the value from the variable's storage any time it's needed in
                  > an expression,[/color]

                  And also writing it.
                  [color=blue]
                  > precluding the compiler from caching the value or some other optimization
                  > WRT the variable's value.[/color]

                  volatile is quite commonly used in low-level code that communicates directly
                  with memory-mapped hardware registers or when transfering data into/out of
                  an interrupt routine. In this case, it's important that any read and write
                  operation is guaranteed to be actually performed. It boils down to: "The
                  as-if rule is not enough."



                  Comment

                  • Phlip

                    #10
                    Re: const volatile???

                    red floyd wrote:
                    [color=blue]
                    > Example:
                    >
                    > const volatile unsigned long * const MEMORY_MAPPED_R EG =
                    > reinterpret_cas t<const volatile unsigned long *>(0xFFE00000L) ;
                    >
                    > MEMORY_MAPPED_R EG is a read only h/w register at FFE00000.  It
                    > can't be written to, but may change due to external issues.[/color]

                    The canonical example here is the system clock...

                    --
                    Phlip
                    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

                    Comment

                    Working...