• Nat (she/they)@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    71
    ·
    2 years 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
      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)

    • QuaternionsRock@lemmy.world
      link
      fedilink
      arrow-up
      7
      arrow-down
      2
      ·
      2 years ago

      I’ve never really thought about this before, but const volatile value types don’t really make sense, do they? const volatile pointers make sense, since const pointers can point to non-const values, but const values are typically placed in read-only memory, in which case the volatile is kind of meaningless, no?

      • rooster_butt@lemm.ee
        link
        fedilink
        arrow-up
        14
        ·
        2 years ago

        They do in embedded when you are polling a read only register. The cpu can change the register but writing to it does nothing.

        • QuaternionsRock@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          2 years ago

          That seems like a better fit for an intrinsic, doesn’t it? If it truly is a register, then referencing it through a (presumably global) variable doesn’t semantically align with its location, and if it’s a special memory location, then it should obviously be referenced through a pointer.

      • Nat (she/they)@lemmy.blahaj.zone
        link
        fedilink
        English
        arrow-up
        3
        ·
        2 years ago

        Maybe there’s a signal handler or some other outside force that knows where that variable lives on the stack (maybe through DWARF) and can pause your program to modify it asynchronously. Very niche. More practical is purely to inhibit certain compiler optimizations.

  • wise_pancake@lemmy.ca
    link
    fedilink
    arrow-up
    68
    arrow-down
    1
    ·
    2 years ago

    Some people hate that C is dangerous, but personally I like its can-do attitude.

    “Hey C, can I write over the main function at runtime?”

    Sure, if you want to, just disable memory protection and memcpy whatever you want there! I trust you.

    It’s a great attitude for a computer to have.

    • mindbleach@sh.itjust.works
      link
      fedilink
      arrow-up
      46
      ·
      2 years ago

      C is dangerous like your uncle who drinks and smokes. Y’wanna make a weedwhacker-powered skateboard? Bitchin’! Nail that fucker on there good, she’ll be right. Get a bunch of C folks together and they’ll avoid all the stupid easy ways to kill somebody, in service to building something properly dangerous. They’ll raise the stakes from “accident” to “disaster.” Whether or not it works, it’s gonna blow people away.

      C++ is dangerous like a quiet librarian who knows exactly which forbidden tomes you’re looking for. He and his… associates… will gladly share all the dark magic you know how to ask about. They’ll assure you, oh no no no, the power cosmic would never pull someone inside-out, without sufficient warning. They don’t question why a loving god would allow the powers you crave. They will show you which runes to carve, and then, they will hand you the knife.

    • SubArcticTundra@lemmy.ml
      link
      fedilink
      arrow-up
      21
      ·
      2 years ago

      Agreed. It’s a very adult approach. C hands you a running chainsaw and whatever happens after that is your responsibility. It is also your responsibility to decide when it’s not the right time to use C.

    • Derpgon@programming.dev
      link
      fedilink
      arrow-up
      9
      ·
      2 years ago

      I loved C/C++ in university, finally the damn piece of rock we forced into thinking was doing exactly what I told him to do, no more and no less.

  • nothacking@discuss.tchncs.de
    link
    fedilink
    arrow-up
    34
    ·
    2 years 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.

  • Kevin@lemmy.world
    link
    fedilink
    English
    arrow-up
    18
    ·
    2 years ago

    If you have a memory-mapped peripheral where there’s a readonly register, I could see it being const volatile.

  • Hobbes_Dent@lemmy.world
    link
    fedilink
    arrow-up
    15
    ·
    2 years ago

    Just spin the pipe wrench open and slide it up then you can switch it back real quick.

    Thank you for watching this OHSA message on bad lockout procedure, now back to your regularly scheduled programming.

  • PriorityMotif@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    2 years ago

    Looks like they didn’t want anybody using the secondary tank. Probably haven’t had time to pull Dave’s body out yet.

  • FruitfullyYours@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    2 years ago

    I’ve used it in the past when having flash memory blocks that could change but you need the compiler to put them into flash memory and not RAM. It’s mainly to get the compiler to stop assuming that it can optimize using the default value.

  • 🐍🩶🐢@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    2 years ago

    laughs in evil PLC programmer A little forces enabled, a change here, and maybe just move this wire over there while I am at it…