• fl42v@lemmy.mlOP
    link
    fedilink
    arrow-up
    8
    ·
    2 years 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
      46
      ·
      2 years 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.

    • TheEntity@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      2 years 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
        ·
        2 years ago

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