Re: [PATCH 02/11] submodule--helper: replace memset() with { 0 }-initialization

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

 



Ævar Arnfjörð Bjarmason <avarab@xxxxxxxxx> writes:

> It's curious that the { 0 } v.s. { NULL } form jumps out at people, but
> seemingly not that you don't memset(&x, NULL, ...). I.e. that we're
> already dealing with a form where C's "0 is NULL in pointer context"
> rules kick in :)

Strictly speaking there are different mechanisms at play here.
Literal "0" spelled in source code and assigned to a pointer
variable assigns a NULL to the variable even on architectures where
the representation of NULL is *not* "all 0-bit filling the width of
the variable" and C language is what guarantees "NULL pointer is
spelled as integer 0 in the source code".

Also, the language, not runtime and pointer representation, is how
second and subsequent members of a struct that are pointers are
initialized to NULL (not necessarily to "all 0-bit filling the width
of the member").

memset(&ptr, '\0', sizeof(ptr)), where ptr is a pointer variable or
a struct/union with a pointer member, on the other hand, is unaware
of how a NULL pointer is represented on the platform.  All it can
(and should) do is to fill the thing "ptr" with all 0-bit.  On an
exotic architecture, where NULL is not "all 0-bit filling the width
of the variable", I do not think it would work.

So from that point of view, using memset() as a replacement for zero
initialization is simply wrong, but in practice people do not work
on such a platform that the distinction matters anymore, hopefully?

> So I wonder if we should say anything about the first member at all.

The mention of the first member of the struct is historically
important only because some checkers like sparse used to complain
about

    struct { TYPE *member0; ... } var = { 0 };

the same way as

    char *string;
    string = 0;

before they got fixed so that they know about "{ 0 }" convention to
be silent about the former, while still warning about "even though C
language says literal 0 is a valid way to spell NULL, you shouldn't
do that" to the latter.

These days, we can safely write "{ 0 }" without having to worry
about the type of the first member.





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux