13 comments

  • andai 58 minutes ago
    Article says pointers and its are both just bitvectors, but doesn't explain what a bitvector is.

    It seems to be a vector of bits? Except in practice, a bitvector is just an int where you're using the binary digits to store stuff? And you gotta use these awkward bitwise operators to actually use it? (Popular in 90s video games, to save RAM!)

    Or was it another way of saying "ultimately it's the same pattern of ones and zeroes so you can do whatever you want with it as long as you know what you're doing" ?

  • kibwen 2 minutes ago
    Recommended further reading in this blog series by Ralf Jung: https://www.ralfj.de/blog/2020/12/14/provenance.html
  • hn_submit 2 hours ago
    Context.
  • bryanlarsen 2 hours ago
    Pointers have provenance, integers don't.
    • dotancohen 1 hour ago
      Go on.
      • bryanlarsen 1 hour 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.

    • pdlclay 20 minutes ago
      [flagged]
  • glitchc 2 hours 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 1 hour 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 1 hour ago
      The "special" thing of note is that GCC considers it acceptable to change the semantics of this program depending on the optimization level.
      • wat10000 57 minutes ago
        That depends entirely on how you define “the semantics.” Any optimizing compiler has to draw a line between what the program is meant to do and how the emitted code actually makes it happen. The proper location of that line is not obvious. Can it turn x++,x++ into x+=2? That’s normally fine, potentially disastrous if x is a memory mapped register on an external device.

        C and C++ happen to have a particularly restrictive view on what “the semantics” of a program really are, such that there are a lot of things you can write in syntactically-legal C that officially erase the semantic meaning of the code. Within that definition of the program’s semantics, gcc’s optimizer is not supposed to change them, and any such change would be a bug. You’re taking a more expansive view of program semantics than the actual language specifies. If you do that, then any optimizing compiler is going to consider it to be acceptable to violate your special bespoke definition of them.

        • marcosdumay 47 minutes ago
          > The proper location of that line is not obvious.

          If that's true, you have a bad language.

          What is known to be the case with C, but this is not a desirable nor a necessary state of affairs. A language can and should have defined semantics.

  • pjmlp 2 hours ago
    2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.
  • m00dy 1 hour ago
    range of values
  • jojogeo 45 minutes ago
    One is a mathematical construct, the other is a dog?
  • stronglikedan 2 hours ago
    well, their names, for starters...
  • spwa4 2 hours ago
    There is no difference.

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

    • HarHarVeryFunny 3 minutes ago
      C doesn't provide any guaranteed that a pointer can be stored in an integer.

      The language only guarantees that an integer is at least 16 bits. There is no guarantee that a compiler that supports a > 16 bit address space will chose to make integers large enough to hold a pointer, and for example gcc still uses 32-bit integers for 64-bit processors.

      I'm not sure if C even requires a pointer to be just a linear address, as opposed to something structured.

    • Ohentis 1 hour 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 1 hour 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 1 hour 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.
        • cryptonector 19 minutes ago
          Sure, but if you're not an embedded programmer, if you're not working with odd architectures, then the difference between pointers and unsigned integers of the same size in C on common current platforms' C ABIs, is minimal. Of course there are footguns here: you can't conjure up and dereference a pointer from an integer that isn't itself obtained from casting from a valid pointer.
    • yxhuvud 1 hour ago
      There is a counter example in the OP.
    • Ungh 1 hour ago
      There's the voice of a C programmer who hasn't used hardware with a different word size to address space...
      • AnimalMuppet 37 minutes ago
        But (almost) every hardware has some integer type that has the same size as what is needed to represent the address space, because (almost) every hardware has some way of manipulating addresses at the assembly level, and (almost) every C implementation has some integer type that generates those assembly instructions.

        I say "almost" because I am not absolutely certain that this is true of absolutely every architecture. Feel free to point out one that breaks these rules, if there is one.

        • cryptonector 17 minutes ago
          In principle a pointer can be a tuple of two other things. Think of CHERI, segmented architectures, etc. In practice on current, modern, general purpose, non-embedded platforms' C ABIs pointers are equivalent to unsigned integers of the same size.
        • Ungh 22 minutes ago
          Segmented 8086 is always the fun one.
    • coolius 46 minutes ago
      I agree, I have passed void* to the JVM by casting to int more often than I can count
    • drfloyd51 2 hours ago
      You should read TFA.
  • cowlevel 1 hour 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 1 hour 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 1 hour ago
        How does gcc document integer to pointer conversion?
        • Maxatar 1 hour ago
          • cowlevel 1 hour ago
            > When casting from pointer to integer and back again, the resulting pointer must reference the same object as the original pointer, otherwise the behavior is undefined.

            So I'm right. This is implementation-defined, and the way the implementation has defined it is to make it fully UB (except for round trips).

            • sham1 0 minutes ago
              Whenever something is undefined behaviour or not is a matter of the standard and what the implementation may or may not have defined for that is immaterial to that. As far as the standard is considered, when they encounter UB, you no longer have a valid C program. However the compiler can still choose to do something with it, which in this particular case manifests as GCC has documented it, but some other compiler can do something else for it.
      • Maxatar 1 hour ago
        [dead]
    • saagarjha 1 hour ago
      No, these are different things.
  • imnotr0b0t 44 minutes ago
    This is interesting