Re: need help..



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
.



Relevant Pages

  • Re: Which PIC18 C Compiler?
    ... Walter gave a good answer regarding recursion in 8051 compilers, totally unrelated to gcc and/or sdcc (since there is no gcc port for the 8051). ... But in my experience on ColdFire's and PPC's, it has generated similar to or better than the few commercial compilers I have seen. ...
    (comp.arch.embedded)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> Ah, but as I've said before, virtually all C compilers on 754 boxes ... >>> happy to use gcc, they all could have used the older gcc spellings ... > use glibc, they all could have used the older glibc spellings. ... for setting traps) interfaces, and I can't be arsed to go through old ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... >> happy to use gcc, they all could have used the older gcc spellings ... parings of C compilers and C runtime libraries are ... use glibc, they all could have used the older glibc spellings. ... trap doesn't actually fire until the _next_ fp operation ...
    (comp.lang.python)
  • Re: [PATCH] kbuild: Disallow GCC 4.1.0 / 4.1.1
    ... but still not clean enough. ... Subject: kbuild: Disallow GCC 4.1.0 / 4.1.1 ... GCC 4.1.0 and 4.1.1 has a bug that can miscompile __weak symbols, ... not allow these compilers (which are quite rare these days, ...
    (Linux-Kernel)
  • Re: What is Linux built with? (or, what does this question mean?)
    ... Isn't the kernel/distro nearly always built w/ gcc? ... Now some of us actually have other compilers as well and these could be ... specific to provide cache and other optimizing that can't be generic. ... gnu compile in a way that it gives some registers over to the kernel as far ...
    (alt.os.linux.suse)