Re: [dwm] malloc'ed client in manage()

From: Szabolcs Nagy <nszabolcs_AT_gmail.com>
Date: Sun, 7 Sep 2008 16:05:05 +0200

On 9/7/08, Matthias-Christian Ott <ott_AT_enolink.de> wrote:
> Nicolas Martyanoff wrote:
>
> Does anyone know whether there are architectures where NULL is not equal
> to 0?

this question occasionally appears on comp.lang.c and there exists
such beast, but i cannot find the reference now

probably it's some old or otherwise esoteric architecture

> See ISO/IEC 9899:TC2 Section 7.17.3:
>
> "The macros are NULL which expands to an implementation-defined null
> pointer constant and; ..."
>
> See ISO/IEC 9899:TC2 Section 6.3.2.3.3:
> "An integer constant expression with the value 0, or such an
> expression cast to type void *, is called a null pointer constant."
>
> Seems like a contradiction in the C99 standard (I have to admit that my
> version is a Committee Draft).

there is no contradiction

'0' can simply mean 2 completely different things: null pointer in
pointer context, or integer 0 otherwise

so a _constant_ integer expression with value 0 automagically becomes
a null pointer which may or may not be represented internally by all
bits zero

(but eg an integer 0 must be represented by all bits zero)

> See ISO/IEC 14882:1998 and ISO/IEC 14882:2003 Section 4.10:
> "A null pointer constant is an integer constant expression (5.19)
> rvalue of integer type that evaluates to zero."
>
> See ISO/IEC 14882:1998 and ISO/IEC 14882:2003 Section 18 Footnote 180:
> "Possible definitions include 0 and 0L, but not (void *)0."
>
> And this sounds like a conflict between C and C++.

yes, c++ was developped with this design decision from the start

> But anyhow it seems that you can't do this reliably, because s isn't
> used by memset as a pointer.
>
> memset(&s, 0, sizeof(s));
>
> where s is a struct.

yes, that's the point: according to standard calloc is not ok, but
default struct initialization is

(but we can overlook this since X won't run such weird architecture)

> According to ISO/IEC 9899:TC2 Section 6.7.8.1 and Section 6.7.8.10 this
> would be correct:
>
> s = {0}; or s = 0;
>
> Anyhow it's not working with gcc.

in c99 you initialize a struct to zero with:

struct_t s = {};
Received on Sun Sep 07 2008 - 14:05:05 UTC

This archive was generated by hypermail 2.2.0 : Sun Sep 07 2008 - 14:12:05 UTC