News for hackers. Stuff that matters.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:53PM (#35952896)
Umm, no. 0 is the null pointer constant, same as nullptr. It is not a location, but an abstraction. If a platforms null pointer happens to be the address 0xFFFFFFFF, then 0 will produce that.

There is no difference between

  char *p = nullptr;
and

  char *q = 0;
other than the variable name; the two have to compare equal: (p == q).

What's wrong with 0 is that when it's not in an expression where it's being converted to integer type, it's just an integer.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by bluGill on Monday May 15, 2023 @06:37PM (#35955454)
The problem is if 0 is a valid pointer and I write

volatile int* x=0; x=0x1234;

Did I just deference the null pointer or make a valid write to that memory location? There is no way to know for sure, you can only apply heuristics to make a guess.

Of course if the lines are that closely spaced you can guess, but in real code they can be in different translation units.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @07:05PM (#35955720)
ISO C says that x isn't a valid pointer for dereferencing, so *x = 0x1234 is undefined behavior. If in your environment that pointer refers to some location where you can put a value, and this is documented, then you're using a documented extension.

If you're using some environment in which x isn't the zero address, but you do want the actual zero address, you need some other way to obtain it, like converting a non-constant expression:

  const int x = 0;
  char *zeroaddr = (char *) x;
C implementations are not required to diagnose null dereferences. In many familiar environments, its arranged by the way the program is loaded. An unmapped page of virtual memory is put at that address. That scheme can be defeated if an offset is involved ptr[large_offset] or ptr->member where member is at a large offset into the struct type.

It's possible to have run-time checks for a null pointer as a compiler option, with a run-time penalty. Other than that, you can use assertions to defend against them.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by userbinator on Monday May 15, 2023 @08:02PM (#35956204)
Your example declares one variable of type "pointer to int" and sets it to some arbitrary value. You didn't dereference anything.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by bluGill on Monday May 15, 2023 @10:59PM (#35957315)
The joys of programming in q comment box on a phone. .. too late to edit, so you just have to pretend i did dereference it
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Tuesday May 16, 2023 @03:31AM (#35958722)
(Like I did.)