Hacker News new | ask | show | jobs
by ChrisSD 2104 days ago
> A pointer is a memory address.

This is an amazingly wrong statement. In assembly you deal with memory addresses. Pointers in C are a much higher level abstraction.

> On current mainstream Intel processors, it occupies four bytes of memory (because an int is four bytes wide).

This depends on the compiler as well as the processor.

4 comments

The Standard disagree.

C11 6.5.3.2p3 “The unary & operator yields the address of its operand. If the operand has type ‘type’, the result has type ‘pointer to type’.”

I understand the intention to warn about the abstraction C introduces, but you’ve confused things.

Pointers and addresses are perfectly covered.

What you really want to bring is what the Standard calls the “C abstract machine”, for which the memory model can be surprising.

> Pointers in C are a much higher level abstraction.

Not really; almost all programmers treat them as addresses and almost all compilers represent them that way. Regardless of what the standard actually says. Leading to surprises when this doesn't hold true, and awkwardnesses of the 16-bit x86 "near/far" pointers.

I mean, that's the only reason that "array[index]" and "index[array]" are interchangeable; you don't see that in other languages.

What higher level abstraction do pointers bring to the table over plain memory addresses other than pointer-arithmetic?
>This depends on the compiler as well as the processor.

Yes. On x64 compilers targeting 64bit cpus, the following prints 8 instead of 4:

  #include <iostream>
  int main () {
      std::cout << sizeof(int*) << std::endl;
  }
Posting C++ in a thread about C is odd. In C it's:

    #include <stdio.h>

    int main(void)
    {
      printf("%zu\n", sizeof (int *));
      return 0;
    }
There's a double-quote character missing.
Thanks. I suck at posting from my phone. Edited.
The article talks about the size of the pointee in memory though, not the size of the pointer.

An 'int' is usually 4 bytes wide when compiling for 64-bit ISAs (at least I haven't seen situations yet where this isn't the case, my experience is limited to x86 and ARM though). Modern C fixes this ambiguity with sized integer types (e.g. int32_t vs int64_t).

>The article talks about the size of the pointee in memory though, not the size of the pointer.

Yes, you're right about the article's text. When I read gp ChrisSD's comment in isolation where he quotes ">A pointer is a memory address" -- followed immediately by him quoting ">On current mainstream Intel processors, _it_ occupies four bytes of memory"

... I thought the "_it_" was referring to a "pointer" instead of plain "int". It didn't occur to me to that the actual article has extra text in between those 2 extracted quotes which drastically changes the assumption of what the pronoun "it" means. My mistake.