News for hackers. Stuff that matters.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by cjensen on Monday May 15, 2023 @01:44PM (#35951836)
nullptr has been such a Godsend for C++. Good to see it coming to C.

If you ever see the macro NULL in code, be afraid. There are two valid ways of defining the macro and that cause weird issues when porting code. For example, in the statement printf ("%p %s\n", NULL, "Hello world!"), one of the definitions leads to NULL being interpreted as the null pointer, and the other leads to NULL being interpreted as an integer. The latter may crash if integer and pointer are different sizes.

It also causes problems with C++ overloading if one overload takes a pointer and another takes an integer.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by josefx on Monday May 15, 2023 @02:18PM (#35952341)
> The latter may crash if integer and pointer are different sizes.

Apparently some compilers specified a special __null extension to handle that case before nullptr was a thing.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:49PM (#35952821)
Since NULL expands to an implementation-defined null pointer constant, it is valid for those implementations to go as far as #define NULL __null.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by LordShredda on Monday May 15, 2023 @01:52PM (#35951939)
But does C need a nullptr keyword? If you're programming in C, you usually define 0 as an invalid value, or a null value. C doesn't have the insane type system C++ has and doesn't have a very strong need to make a distinction between a pointer or an integer, since they're all in the end numbers.

The printf example you gave is an example of garbage in, garbage out. If NULL is a macro not defined as a pointer sized integer, then you're at fault here.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by mananaysiempre on Monday May 15, 2023 @02:11PM (#35952233)
> If NULL is a macro not defined as a pointer sized integer, then you're at fault here.

If it was you who wrote stdlib.h, sure; otherwise, if you’re on a platform where NULL is traditionally defined as 0 and not (void *)0, you’re stuck. A conformant implementation is free to use either definition.

If you want to language-lawyer more heavily, C does not require there to be pointer-sized integers (uintptr_t is optional), does not require that all zero bytes represent a null pointer in memory (unlike for integers), does not require that the implementation choose to store an integer with value zero as all zero bytes (there may be other valid representations), and in any case does not require an implementation to do anything reasonable at all if the caller passes an integer but a vararg callee looks for a pointer (think separate integer and pointer registers).

(I’m not entirely sure if (void *)(void *)0 is a null pointer constant—does it count as a zero-valued integer constant expression cast to a pointer to void? So you might not even be able to use (void *)NULL as a hedge against bad platform headers.)

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:51PM (#35952861)
You're allowed to do:

  #undef NULL
  #define NULL ((void *) 0)
Just don't do it prior to the inclusion of any standard header (C or POSIX).
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by mananaysiempre on Monday May 15, 2023 @03:46PM (#35953511)
I don’t think you are? Redefining a reserved identifier is UB per ISO C (any version) 7.1.3p2, and per 7.1.3p1,

> Each macro name in [the standard library] is reserved for use as specified if any of its associated headers is included; unless [you’re #undef’ing a function also provided as a macro].

The general idea seems to be that standard headers are allowed to use macros they define, even in other macros they define, and because macro names are late-bound, even if the user only redefines the name afterwards, every macro that uses it will then be affected.

As a silly example, a valid part of stdlib.h could be

  #define NULL 0
  #define EXIT_FAILURE (NULL)
  #define EXIT_SUCCESS (EXIT_FAILURE+1)
and now after your redefinitions EXIT_SUCCESS becomes a constraint violation.

(For an implementor to actually do this would of course be dumb, but you did say “allowed”, and that’s what the standard says here.)

Or did I misunderstand “use as specified”?

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @07:48PM (#35956098)
Do it where it works (hopefully some platform for which you build all the time), and get your enhanced diagnostics there; avoid it where there are problems like this.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by LegionMammal978 on Monday May 15, 2023 @02:01PM (#35952100)
Well, the fault depends on who "you" are: the NULL macro generally comes from one's libc, and allegedly some libc maintainers have been very obstinately against changing their NULL macros to have pointer type.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by throwway120385 on Monday May 15, 2023 @02:02PM (#35952113)
Aren't there platforms where pointers have additional type or space information encoded that is orthogonal to the numeric address? It's only by convention that NULL == 0 because on platforms like Intel & ARM you would typically not use the first page. But that's only a convention, and you could just as easily put a null page at the top of your address space, especially in systems with an MMU where mappings can be added, removed, or remapped as-needed.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by mananaysiempre on Monday May 15, 2023 @02:21PM (#35952404)
> It's only by convention that NULL == 0 [...] and you could just as easily put a null page at the top of your address space [...].

Technically NULL == 0 always because the standard special-cases zero-valued constant expressions; (uintptr_t)NULL == 0 or NULL == *(void **)calloc(1, sizeof(void *)) is another matter :)

Language lawyering aside, a non-all-zeroes representation of NULL will probably blow up most C programs [e.g. static-storage-duration initialization is now not the same as calloc or memset(,0,) and is even type-specific]. Like CHAR_BIT, that’s a joint that technically exists but has been rusted for decades (pun not intended).

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:59PM (#35952972)
There is no problem with static initializations with a null pointer that is not all zero bits, or a floating-point 0.0 that is not all zero bits.

Those values just cannot participate in the "BSS" trick, whereby everything that is zero-initialized is put into a special section that doesn't actually exist in the program image, and is only provided on startup.

Those values would go into the initialized data section.

The problem with 0.0 or null pointers not being all zero bits is all the code that uses calloc or memset zero.

If this is on some specialized platform (e.g. DSP chip), it might not matter that vast quantities of C code are not portable.

In general, compiler (and to a great extent instruction set architecture!) designers are quite hamstrung by the expectations of C programmers and programs; that has been the situation for some thirty years now.

Today, you could not sucessfully introduce a system in which pointers to bytes (void , char ) have a different representation from other pointers (let alone different size, lord forbid).

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by pjmlp on Monday May 15, 2023 @04:20PM (#35953962)
If we if ignore ongoing efforts on hardware memory tagging since SPARC ADI.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @06:51PM (#35955580)
Hardware memory tagging schemes carefully avoid breaking common C idioms.

What breaks are

- truly dubious programs which use pointers in ways they really shouldn't, and are almost certainly just bugs; like use-after-free. When a pointer with an out-of-date tag is passed to the library, that is pretty much a confirmed use-after-free, which is not a legitimate idiom of non-maximally-portable C, like assuming that all pointers are the same size.

- C programs which manipulate the pointer representation: e.g. run-times for dynamic languages that put their own tags in pointers. These can easily work around tagging. (I've dealt with this on Android recently, quite easily. For the affected objects, I strip the tag away, and work with the untagged pointers. When it's time to pass the pointer back to Android's library, I put the tag back. All other code works as before.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by pjmlp on Tuesday May 16, 2023 @01:05AM (#35957919)
Yeah, but all of that falls off ISO C defined semantics.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by mananaysiempre on Tuesday May 16, 2023 @08:03AM (#35960630)
Incidentally, AFAICT C23 now allows stashing log2 alignof(max_align_t) bits in pointers in a portable (if awkward) manner: for example, for a char pointer p, the lowest such bit can be retrieved with memalignment(p)<2 and masked off with p-(memalignment(p)<2).

(C++’s std::aligned would be less awkward, but, well, memalignment() is what we get.)

You still can’t portably store pointers and integers (or doubles) in the same place and distinguish them, though.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by LegionMammal978 on Monday May 15, 2023 @02:12PM (#35952243)
In C, the integer 0 is explicitly defined to convert to a null pointer for all assignments, casts, comparisons, etc., regardless of what the pointer's "actual" value is. The only time where you can see that a null pointer doesn't have numeric value 0 is when you manipulate its object representation with memset, memcpy, etc. The compiler is also at liberty to return whatever it wants when you convert a null pointer to an integer, except that converting it back must produce a null pointer (if it's at least as wide as intptr_t).
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kevin_thibedeau on Monday May 15, 2023 @02:44PM (#35952748)
0 never should have been overloaded in C to refer to the NULL pointer. With pointer assignment and comparison it transforms to the platform's encoding for NULL which isn't necessarily all zeros. No other literal has this sort of magic.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:54PM (#35952914)
The C language existed before there was a preprocessor which made it possible to define NULL.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kevin_thibedeau on Monday May 15, 2023 @03:13PM (#35953187)
This has nothing to do with the preprocessor. The concept of NULL existed before the macro was standardized. Literal zeros were the way to refer to it which was a design mistake.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @07:19PM (#35955844)
I think it's a fine design. Obviously, the C++ people who invented

  virtual void fun() = 0;
were blind to whatever mistake it embodies.

When I gained the proper understanding that 0 is a null pointer constant, I immediatelly stopped using NULL. It's so much nicer.

- It works fine in a typeless language in which every value is a machine word, and integers and the null pointer share exactly the same representation.

- It works fine in a strongly typed language, where context indicates what zero means, even in the context of different types of different sizes and representations.

Unfortunately C (and also C++) contains some untyped areas, like variadic argument lists.

I would have designed it differently: only the token 0 would have the special overloaded meaning, and not all constant zero-valued expressions of any integer type. So (0 + 0) would not be a null pointer.

In unsafe contexts, the use of a 0 token would be diagnosed.

   printf("...", ... 0, ...); // diagnosed

   printf("...", ... 0L, ...); // not diagnosed: argument of long type

   printf("...", ... 0 + 0, ...); // not diagnosed: argument of int type

   printf("...", ... (void *) 0, ...); // not diagnosed: null pointer of (void *) type.
I think I would also not have the hexadecimal or octal zero tokens be the null pointer constant:

   char *p = 0; // null pointer, no diagnostic
   char *q = 0x0;  // error, integer to pointer with no cast
The downside is that the AST would have to retain that representation detail somewhere (possibly just in a single Boolean flag).
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kevin_thibedeau on Monday May 15, 2023 @08:53PM (#35956532)
This isn't just a C++-ism. The null pointer constants are only more prominent in C++ because of the rejection of void * even though it isn't any "safer" to have a special integer literal with the same semantics. Ultimately, it all comes from K&R C.

There are legitimate cases where you need a function pointer assigned to address zero (reset vectors commonly). The correct behavior in C is ambiguous if the null address isn't also 0 since the standard doesn't call out special behavior for function pointers. That wouldn't be the case if nullptr had been standardized earlier and there was no need for the magic 0 as a null pointer constant.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by WalterBright on Monday May 15, 2023 @02:56PM (#35952945)
This works in Standard C:

    int *p = 3;
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by LegionMammal978 on Monday May 15, 2023 @03:10PM (#35953137)
That's a compiler extension. In C17, 6.5.16.1 (Simple assignment) implies that the RHS of an assignment to a pointer must either have pointer type or be a null pointer constant (i.e., an integer constant equal to 0, or such a constant casted to pointer type), and 6.7.9 (Initialization) states that "the same type constraints and conversions as for simple assignment apply" to expressions used as initializers.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by Kamq on Monday May 15, 2023 @11:27PM (#35957448)
"standard" C implies default to me, so the post is probably referring to C89 or C99, which are significantly more used than C17.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by LegionMammal978 on Tuesday May 16, 2023 @02:02AM (#35958197)
This particular rule is essentially unchanged from C89 (3.5.7 for initialization, 3.3.16.1 for simple assignment) and from C99 (6.7.8 for initialization, 6.5.16.1 for simple assignment). Generally, most existing rules don't change much at all across the C Standard versions.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by ynik on Tuesday May 16, 2023 @04:59AM (#35959243)
"Standard C" is the C that was standardized. This is not what you get from your compiler by default!

With gcc, you have to use `gcc -std=c99 -pedantic-errors`, otherwise it will accept programs which are invalid according to the standard.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kevin_thibedeau on Monday May 15, 2023 @03:04PM (#35953056)
The address is still 3 which has valid applications. C is permissive enough to run on platforms that don't use address 0 for NULL. With pointer operations the compiler will change the encoding from 0 to that platform's NULL address.

  int *p = 0;
  intptr_t i = (intptr_t)p;
  
  if(i == 0) ... // Isn't always true
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by Gibbon1 on Monday May 15, 2023 @03:24PM (#35953293)
I like to point out that AVR micro's reading and writing to address 0 is legit.

On the ARM Cortex I think address 0 is the initial stack pointer value.

My opinion is NULL being something special in the language is mathy CS academics trying to turn C into a mathy abstract language. Which it ain't.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by jcelerier on Monday May 15, 2023 @04:32PM (#35954093)
C is officially a mathy abstract language since 1989, the compilers just took some time to catch up.

> 2.1.2.3 Program execution

> The semantic descriptions in this Standard describe the behavior of an abstract machine [...]

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by Gibbon1 on Tuesday May 16, 2023 @02:25AM (#35958322)
The mathy version is C is useless for real programs.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by bluGill on Monday May 15, 2023 @02:45PM (#35952760)
Assuming 0 is an invalid value is not always correct. 0 is a perfectly valid pointer, and making it impossible to refer to that location is bad. Of course if you are not writing an OS or embedded system you won't ever have a pointer value of 0 anyway as the OS can put things elsewhere with no problem (if you are you need to see your CPU docs, some CPUs 0 is invalid, some it is not).
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.)
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by roqi on Monday May 15, 2023 @03:07PM (#35953088)
> But does C need a nullptr keyword?

Yes, it does.

> If you're programming in C, you usually define 0 as an invalid value, or a null value.

That was also the usual pattern in C++ when there was no alternative. Once nullptr was introduced in C++, NULL or 0 quickly became a code smell.

> C doesn't have the insane type system C++ has and doesn't have a very strong need to make a distinction between a pointer or an integer, since they're all in the end numbers.

C++'s type system is far from insane. It's actually one of it's killer features.

You're both entirely oblivious to the need to not conflate pointers with integers and failing to present any case in favour of the legacy and broken use of NULL, and in the process failing to address all family of known error patterns involving it.

> The printf example you gave is an example of garbage in, garbage out. If NULL is a macro not defined as a pointer sized integer, then you're at fault here.

Again, you seem to be completely oblivious to the problem domain. NULL is not a macro as far as C or C++ compilers are concerned. NULL is a magic constant that's resolved at preprocessing time. Replacing NULL with nullptr means a magic constant is replaced by a concrete type, and thus whole family of errors can be avoided with compile time checks. Claiming that the developers who wrote in bugs are at fault for inadvertently adding bugs makes no sense at all because it does not solve any problem at all, and instead is just cynical finger pointing. I take compile-time checks over unhelpful finger pointing all day every day.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by colonwqbang on Monday May 15, 2023 @05:24PM (#35954675)
NULL is a macro.

The original mistake by the standards committee was allowing implicit conversions from integer to pointer. I.e. allowing NULL to be defined as simply 0.

If NULL had been defined always as ((void *) 0) then I don't see that we would have had a problem.

But that's all history now and in this situation I can see that adding nullptr becomes a reasonable way out.

It's ironic though that the fix for the different ways to write null is to add yet another way.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by roqi on Monday May 15, 2023 @06:18PM (#35955265)
> NULL is a macro.

You're missing the whole point.

As per the C standard, NULL is an implementation-defined null Pointer constant.

Macros are resolved in the preprocessing step. The compiler does not know what a macro is. What the compiler knows is whatever the preprocessor passes off in place of the macro. This means the compiler only sees a constant, and has no way to tell what that constant means.

If instead of passing random pointer constants you pass an actual type, now the compiler can tell more things.

> If NULL had been (...)

Irrelevant. The whole point is that it wasn't the committee looked at the problem, and it determined that using a dedicated type is safer, more powerful, and more elegant than passing magic numbers around.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by ori_b on Tuesday May 16, 2023 @12:30AM (#35957767)
I am on the committee. As you can probably tell by my posts, this was not unanimous.

I'd categorize nullptr as 'mostly harmless'

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @02:48PM (#35952810)
The problems are that:

- NULL is idiomatic: using NULL is entrenched in C programming and it is not going away.

- In spite of nullptr existing now, NULL is still (quite stupidly) not required to just expand to nullptr, but to an implementation-defined null pointer constant, rather than #define NULL nullptr. (According to the N2596 draft).

- They had over 30 years to tighten the requirements on how NULL can be defined; what's the matter? C99 could already have required NULL to be ((void *) X) where X is an integer-typed constant expression evaluating to zero.

I'm not going to start using nullptr. It's not idiomatic C. I'm going to hold out hope that NULL will be fixed so that it expands to nullptr.

--

Also, it's possible for a compiler to diagnose when a constant, zero-valued expression is used as the argument of a variadic function. The diagnostic can be confined to cases when such a constant expression is the result of macro expansion:

  printf_like_function("fmt ...", ... 0, ...);  // OK

  #define FOO 0

  printf_like_function("fmt ...", ... FOO, ...);  // compiler diagnostic

  printf_like_function("fmt ...", ... (int) FOO, ...); // OK
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by bigbillheck on Monday May 15, 2023 @03:55PM (#35953619)
> NULL is idiomatic

It is today, but idioms are a human concept and who knows how things will be in 2033?

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kllrnohj on Monday May 15, 2023 @06:44PM (#35955527)
Well hopefully at some point we'll stop writing C at all
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by bigbillheck on Monday May 15, 2023 @07:40PM (#35956021)
Inshallah some day, but I don't see it happening in what's left of my career.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by pjmlp on Monday May 15, 2023 @04:16PM (#35953903)
Once upon a time K&R C function declarations were idiomatic, in C23 they are out.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by cogman10 on Monday May 15, 2023 @04:54PM (#35954345)
> In spite of nullptr existing now, NULL is still (quite stupidly) not required to just expand to nullptr, but to an implementation-defined null pointer constant, rather than #define NULL nullptr. (According to the N2596 draft).

This is so silly. I sort of get why not (can't break the dork that decided to do

    int i = NULL;
    i++;
)

But, at the same time... I almost feel like this is a "you are being a dork, go fix your code." moment. This isn't the sort of break where someone would see it and go "Oh yeah, assuming NULL is anything other than nullptr is dumb!"

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kazinator on Monday May 15, 2023 @06:32PM (#35955405)
> can't break the dork that decided to

Why not? We've broken the dork who used undeclared functions, void main, gets ...

(It's the same funking dork anyway. You know who you are, I'm looking at you!)

Note that

  int x = ((void *) 0);
will actually work in GCC and get you a zero into x, just with a conversion warning. The dork is unaffected; their code works and they don't read warnings.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by GabrielTFS on Wednesday May 17, 2023 @11:00AM (#35976240)
See https://gcc.gnu.org/pipermail/gcc/2023-May/241264.html for why this might not be the case anymore soon - although I suppose that adding in -fpermissive or -Wno-error=conversion or something like that isn't too much effort.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by ori_b on Monday May 15, 2023 @03:07PM (#35953091)
> If you ever see the macro NULL in code, be afraid. There are two valid ways of defining the macro

Not on a Posix system, where the only valid definition of it is `(void*)0`. C could have adopted this definition.

Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void*` conversions.

C doesn't have this problem.

Adopting the Posix definition of NULL in the standard would have been sufficient -- and unlike `nullptr`, would have solved bugs in existing programs.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by blackpill0w on Monday May 15, 2023 @03:44PM (#35953481)
>C doesn't have this problem

I don't think a strong type system is a `problem`, implicit conversions can lead to so many annoying and hard to find bugs.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by ori_b on Monday May 15, 2023 @03:46PM (#35953510)
That's a fine general sentiment. However, in this context it's a problem if you want to assign NULL to a pointer without a cast, which is why C++ added the magically convertible nullptr to add to the the magically convertible `0` constant.

     char *x = 0; // ok in C and C++
     char *y = (void*)0; // ok in C, error in C++
     char *z = nullptr; // ok in C++
This prevents C++ from defining NULL the way that Posix C does.

I don't understand why it's more acceptable to allow magic `0` conversions than magic `(void*)0` conversions, given that the latter is far less likely to happen by accident -- but here we are.

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by rightbyte on Monday May 15, 2023 @05:13PM (#35954564)
> I don't understand why it's more acceptable to allow magic `0` conversions than magic `(void*)0` conversions

In the end you don't have to chose between '0' and 'nullptr' anyways.

    char *x = (decltype (nullptr)) 0;
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by kzrdude on Monday May 15, 2023 @03:47PM (#35953520)
Case in point, integer arithmetic in C. Reasoning about types there is just tiring.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by Dylan16807 on Monday May 15, 2023 @06:06PM (#35955160)
They didn't say the type system is a problem, they said it caused a problem.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by haileys on Monday May 15, 2023 @07:26PM (#35955917)
> Nullptr is needed in C++ because `0` is the only definition of `NULL` that works with the type system, due to the lack of implicit `void` conversions.*

> C doesn't have this problem.

Except for conversions between data pointers and function pointers ;)

Initialization seems to be a special case, but with `-pedantic` the following code will show a warning on the initialization of `fp2`:

    void* return_null() {
        return 0;
    }
    
    int main() {
        void(*fp1)() = 0;
        void(*fp2)() = return_null();
    
        return 0;
    }
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by saagarjha on Monday May 15, 2023 @07:41PM (#35956034)
POSIX requires this to be supported as well.
Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by whimsicalism on Monday May 15, 2023 @08:53PM (#35956535)
> due to the lack of implicit `void` conversions. C doesn't have this problem.

implicit void conversions are the problem

Re:GCC 13 Supports New C2x Features, Including Nullptr, Enha... by ori_b on Tuesday May 16, 2023 @12:27AM (#35957745)
Give me a legitimate use for void* that isn't a conversion.