News for hackers. Stuff that matters.
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.