9 comments

  • pjmlp 1 hour ago
    2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.
  • glitchc 42 minutes ago
    I don't think GCC is doing something special, rather that -O3 might be optimizing out the subtraction. Try with -O0, I suspect you will get the same result as clang.
    • 8-prime 21 minutes ago
      The GCC compiler changes the order of the operations such that the assignment happens after the print for x.

      call __printf_chk@PLT mov ecx, DWORD PTR 0[rbp] mov rdx, rbp mov edi, 2 mov DWORD PTR -96[rbx], 7

    • mkarrmann 7 minutes ago
      The "special" thing of note is that GCC considers it acceptable to change the semantics of this program depending on the optimization level.
  • bryanlarsen 50 minutes ago
    Pointers have provenance, integers don't.
    • dotancohen 26 minutes ago
      Go on.
      • bryanlarsen 14 minutes ago
        I tried, but frankly, typing "pointer provenance" into Google gets you a much better explanation than my attempt. Google's AI analysis might be questionable, but it's less questionable than my first attempt.

        To oversimplify, provenance is the metadata a compiler keeps about a pointer during the compilation process.

  • m00dy 10 minutes ago
    range of values
  • stronglikedan 42 minutes ago
    well, their names, for starters...
  • spwa4 46 minutes ago
    There is no difference.

    Yes, I'm a C programmer, how did you know?

    • Ohentis 37 minutes ago
      It doesn't sound like you are or you would know C doesn't define either precisely enough to make that statement true.
    • yxhuvud 23 minutes ago
      There is a counter example in the OP.
    • Ungh 30 minutes ago
      There's the voice of a C programmer who hasn't used hardware with a different word size to address space...
    • drfloyd51 42 minutes ago
      You should read TFA.
  • hn_submit 59 minutes ago
    Context.
  • cowlevel 28 minutes ago
    > First, does this C program execute undefined behavior? It does not! Computing out-of-bounds pointers is UB but we are free to do whatever we like with integer-typed values that were derived from pointers. Second, does the C standard mandate the behavior that we saw here? It does not!

    Behaviour that is not defined is undefined behaviour. Integer to pointer conversion is IDB, which is the same as UB except that a conforming compiler documents what could happen. I believe it's typically documented as unpredictable, so the same as plain UB.

    • tsimionescu 5 minutes ago
      There is a vast difference between IDB and UB. IDB is perfectly fine to rely on for your compiler, but it means your program may not be portable to other compilers.

      UB means your program is not a C program and has no semantics whatsoever past the point it reaches the UB. The compiler is thus free to assume that the point that the UB would happen can't be reached and optimize in accordance to that assumption - which many modern compilers do, with often unexpected consequences (such as the infamous case of removing an integer overflow check in Linux because an_int+1>an_int is guaranteed to be true in the C abstract model).

      • cowlevel 3 minutes ago
        How does gcc document integer to pointer conversion?
    • saagarjha 28 minutes ago
      No, these are different things.