Re: need help..
- From: Chris Croughton <chris@xxxxxxxxxxxx>
- Date: Fri, 1 Apr 2005 16:23:41 +0100
On Thu, 31 Mar 2005 23:43:41 GMT, CBFalconer
<cbfalconer@xxxxxxxxx> wrote:
> Nils Weller wrote:
>>
>> 3) Quality compilers warn about the ``if (foo = 0)'' construct. I know
>> for a fact only that gcc and HP's compilers do this (and not because the
>> bug ever occured to me, but because I explictly tested these compilers),
>> but it is very likely that the offerings of vendors such as Comeau,
>> Compaq, Intel, SGI, IBM, Sun and a host of others are also capable of
>> doing this (I don't have these compilers handy right now and will let
>> others fill in the blanks.)
>
> And I dislike that warning. I often want to write something like:
>
> if (!(p = malloc(sizeof *p))) getoutahere("no memory");
> else if (!(p->ptr = malloc(sizeof *p->ptr))) {
> free(p); getoutahere("no memory");
> }
> else {
> /* all seems well, go for it */
> }
> /* Unless getoutahere does something, we always get here */
>
> and similar things to do with opening files, getting parameters,
> etc.
Quality compilers (GCC derived ones, for example) note the extra
parentheses around the assignment and don't produce the warning:
if (p = q) ... /* warning */
if ((p = q)) ... /* no warning */
if (!(p = q)) ... /* no warning */
The first could be a mistake, the second and third are likely to be
intentional.
Although I prefer the clearer:
p = q;
if (p) ...
in most cases (idioms with for and while loops and inputting data are
about the only times I use the contraction, I don't do it in an if
test).
Chris C
.
- References:
- need help..
- From: slickn_sly
- Re: need help..
- From: Martin Ambuhl
- Re: need help..
- From: CBFalconer
- Re: need help..
- From: Nils Weller
- Re: need help..
- From: CBFalconer
- need help..
- Prev by Date: Re: Use of assert.h
- Next by Date: Re: type of array index?
- Previous by thread: Re: need help..
- Next by thread: Re: need help..
- Index(es):
Relevant Pages
|