9 comments

  • pjmlp 1 hour ago
    2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.
  • glitchc 56 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 35 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 21 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 1 hour ago
    Pointers have provenance, integers don't.
    • dotancohen 39 minutes ago
      Go on.
      • bryanlarsen 28 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 24 minutes ago
    range of values
  • stronglikedan 56 minutes ago
    well, their names, for starters...
  • spwa4 1 hour ago
    There is no difference.

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

    • Ohentis 51 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.
      • Maxatar 7 minutes ago
        On the contrary, the vast majority of C programmers don't know C well enough to understand the difference between integers and pointers.

        Using a tool doesn't mean understanding that tool, just means you know how to follow some repetitive processes over and over again.

      • kevin_thibedeau 7 minutes ago
        They are definitely not integers on the Symbolics Lisp machine. The C standard allows for atypical representations. [u]intptr_t is optional for this reason.
    • yxhuvud 37 minutes ago
      There is a counter example in the OP.
    • Ungh 44 minutes ago
      There's the voice of a C programmer who hasn't used hardware with a different word size to address space...
    • drfloyd51 56 minutes ago
      You should read TFA.
  • hn_submit 1 hour ago
    Context.
  • cowlevel 42 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 19 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).

      • Maxatar 0 minutes ago
        That's simply false. You're mixing up what the C standard guarantees as a requirement with whether a program has semantics at all.

        The C standard in S 3.4.3 defines undefined behavior as behavior for which the standard imposes no requirements. It does not say the program ceases to be a C program or that it has no semantics whatsoever. In fact, the standard explicitly acknowledges that undefined behavior can include an implementation behaving in a documented manner characteristic of the environment.

        Saying that "the C standard doesn't define the behavior" is correct. Stretching it to "the program has no semantics whatsoever" is not and those are two very different claims for which the C standard itself explicitly differentiates.

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