• zea@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    72
    ·
    6 months ago

    It makes more sense if you think of const as “read-only”. Volatile just means the compiler can’t make the assumption that the compiler is the only thing that can modify the variable. A const volatile variable can return different results when read different times.

    • fl42v@lemmy.mlOP
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      6 months ago

      I thought of it more in terms of changing constants (by casting the const away). AFAIK when it’s not volatile, the compiler can place it into read-only data segment or make it a part of some other data, etc. So, technically, changing a const volatile would be less of a UB compared to changing a regular const (?)

      • Scoopta@programming.dev
        link
        fedilink
        arrow-up
        51
        ·
        6 months ago

        const volatile is used a lot when doing HW programming. Const will prevent your code from editing it and volatile prevents the compiler from making assumptions. For example reading from a read only MMIO region. Hardware might change the value hence volatile but you can’t because it’s read only so marking it as const allows the compiler to catch it instead of allowing you to try and fail.

          • Suzune@ani.social
            link
            fedilink
            arrow-up
            19
            ·
            edit-2
            6 months ago

            When you program embedded you’ll also dereference NULL pointers at some point.

            More...

            Some platforms can have something interesting at memory address 0x0 (it’s often NULL in C).

            • Scoopta@programming.dev
              link
              fedilink
              arrow-up
              11
              ·
              6 months ago

              In amd64/x86 kernel space you can dereference null as well. My hobby kernel keeps critical kernel structures there XD.

            • humbletightband@lemmy.dbzer0.com
              link
              fedilink
              arrow-up
              2
              ·
              edit-2
              6 months ago

              I was thinking about telling them how in embedded systems it’s a good practice to allocate the memory by hand, having in mind the backlog, but yours will come first

      • mox@lemmy.sdf.org
        link
        fedilink
        arrow-up
        16
        ·
        edit-2
        6 months ago

        AFAIK when it’s not volatile, the compiler can place it into read-only data segment

        True, but preventing that is merely a side effect of the volatile qualifier when applied to any random variable. The reason for volatile’s existence is that some memory is changed by the underlying hardware, or by an external process, or by the act of accessing it.

        The qualifier was a necessary addition to C in order to support such cases, which you might not encounter if you mainly deal with application code, but you’ll see quite a bit in domains like hardware drivers and embedded systems.

        A const volatile variable is simply one of these that doesn’t accept explicit writes. A sensor output, for example.

      • TheEntity@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        6 months ago

        The very notion of “less of a UB” is against the concept of UB. If you have an UB in your program, all guarantees are out of the window.

        • fl42v@lemmy.mlOP
          link
          fedilink
          arrow-up
          2
          ·
          6 months ago

          I mean, changing a const is itself a questionable move (the question being whether the one doing it is insane)

  • nothacking@discuss.tchncs.de
    link
    fedilink
    arrow-up
    34
    ·
    6 months ago

    This is actually how you should declare something that you will never change, but something might change externally, like an input pin or status register.

    Writing to it might do something completely different or just crash, but you also don’t want the compiler getting creative with reads; You don’t want the compiler optimizing out a check for a button press because the “constant” value is never changed.

    • sunbeam60@lemmy.one
      link
      fedilink
      arrow-up
      3
      ·
      6 months ago

      Yeah I stumbled on this too. Surely the joke should be const mutable, not const volatile.

    • blackstrat@lemmy.fwgx.uk
      link
      fedilink
      arrow-up
      2
      ·
      6 months ago

      Volatile means that the value should be read each time its accessed. It can’t be cached in a register or the read be otherwise assumed and optimized away or the instructions around its access be reordered.

  • JATtho@sopuli.xyz
    link
    fedilink
    arrow-up
    1
    ·
    6 months ago
    volatile int blackhole;
    blackhole = 1;
    const int X = blackhole;
    const int Y = blackhole;
    

    Compiler is forbidden to assume that X == 1 would be true. It’s also forbidden to assume that X == Y. const just means the address and/or the data at the address is read only. const volatile int* const hwreg; -> “read only volatile value at read only address hwreg”. Compiler can assume the hwreg address won’t magically change, but can’t assume the value read from that address won’t.