On Fri, 6 Oct 2006, Chris Lightfoot wrote:
> Relevant references (from the C99 final committee draft
> but I think these carry over to the real standard):
Ah. I am sufficiently ancient to be still working from the C90 standard.
> 6.8.4.1.2
> if (x) ...
> is equivalent to
> if (x != 0) ...
C90 doesn't say that explicitly, but I suppose it implies it. It says
that the expression must be of scalar type, and that it is compared to
zero.
> 6.3.2.3
> ``An integer expression with the value 0, or such an
> expression cast to type void*, is called a null
> pointer constant. If a null pointer constant is
> assigned to or compared for equality to a pointer, the
> constant is converted to a pointer of that type.''
Aha! That text does indeed also appear in C90 3.2.2.3. I had overlooked
it. It does seem to mandate that NULL is the same as zero. So I have to
concede that if(x) where x is a pointer is standard-conforming code.
However, I still don't like using if(x) where x is a pointer because
when you come across if(x) in the middle of code, it gives you no clue
as to what type x is, whereas if(x==NULL) makes it clear that x is a
pointer and if(x==0) makes it clear that x holds a number. *That*'s my
real point.
> Actually from a brief grep exim isn't free of assumptions
> about the in-memory format of different objects. e.g.
> demime.c does,
>
> memset(&mime_part_p,0,sizeof(mime_part));
>
> to clear the contents of the struct mime_part mime_part_p.
> (There are various other uses of memset(p, 0, sizeof *p)
> but I didn't check whether the elements of the p include
> pointers specifically.) Of course that assumes that the
> appropriate zero or null value for each of the elements of
> the struct is all-bits-zero. That's not necessarily true
Indeed. I wonder if I wrote any of those? (I didn't write the one that
you quote.) My gut feeling is that I would only do that if all the items
in the structure were integers, but you never know. :-) ... Curiosity
got the better of me, and I checked ... looks like the only dodgy cases
are where I've done it for the system structs addrinfo, sockaddr, and
stat.
--
Philip Hazel University of Cambridge Computing Service
Get the Exim 4 book: http://www.uit.co.uk/exim-book