Re: "Using plain integer as NULL pointer" false positive for zero struct initializer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 9, 2019 at 5:32 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> (because NULL literally has special meaning even in C, even if the
> special meaning is less than the special meaning in C++).

Just to clarify: a lot of people seem to think that

    #define NULL ((void *)0)

means that NULL just behaves as a "void *" and is thus compatible with
all other pointers, and that's it.

Nope. Even in C, NULL is actually special in ways that have nothing to
do with "void *" part. You should think of the "(void *)" part as just
syntactic sugar, that makes sure it cannot be mistaken for a regular
integer, and helps turn it into the special thing that NULL is.

It's just way less _noticeably_ special than it is in C++.

In particular, it's special wrt the ternary operator.

If you have

    void *a;
    int *b;

then the expression

    cond ? a : b;

has type 'void *'.

But

    cond ? NULL : b;

has type "int *".

Notice how NULL - even in C - doesn't act as "just another void
pointer". It really is its own special value, and has its own rules
for typing, _outside_ of the rules of "void *" pointers.

This was always true, btw. The C++ people who told you that you couldn't do

    #define NULL ((void *)0)

in C++, because NULL is not just another void pointer were full of
shit.  ANSI C did it, and did it right. Long long long before the C++
people invented a completely pointless new syntax for the same thing.

It's sad that this has to be stated even in the year 2018.

              Linus



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux