Re: Casting the return value of malloc()...
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 25 Nov 2009 08:18:16 -0800
cri@xxxxxxxx (Richard Harter) writes:
On Tue, 24 Nov 2009 15:48:36 -0800, Keith Thompson
<kst-u@xxxxxxx> wrote:
cri@xxxxxxxx (Richard Harter) writes:
[...]
BTW if the, ah, operand might be a macro consider using[...]
parentheses as a prophylactic.
If the operand is a macro, and the macro is defined in such a way that
parentheses are necessary, *fix the macro definition*.
I'd use prophylactic parentheses only if I *knew* that the particular
macro were poorly defined and I was unable to fix it.
That sounds rather odd. You have a choice of two idioms, (a)
enclose the operand in parentheses or (b) omit the parentheses.
Idiom (a) is more robust. You insist on using idiom (b) to the
point of changing existing working code elsewhere rather than
using the more robust idiom (a). This doesn't sound like good
practice to me.
Here's my standard example of preprocessor abuse:
#include <stdio.h>
#define SIX 1+5
#define NINE 8+1
int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}
This is obviously silly, but you can imagine a more realistic example.
Suppose the macros SIX and NINE (assume better names) are provided by
some header that I didn't write. Should I write
printf("%d * %d = %d\n", (SIX), (NINE), (SIX) * (NINE));
to allow for the possibility that the library's author is either
incompetent or malicious? Is there any more reason to do so
if the operator is "sizeof" rather than "*"?
If you write a macro that's intended to be used in an expression
context, it's your responsibility to provide parentheses as needed,
so the user doesn't need to worry about it. (For the standard
headers, this is explicitly required.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- Common misconceptions about C (C95)
- From: Ioannis Vranos
- Re: Common misconceptions about C (C95)
- From: Michael Tsang
- Re: Common misconceptions about C (C95)
- From: Keith Thompson
- Casting the return value of malloc()...
- From: Kenny McCormack
- Re: Casting the return value of malloc()...
- From: Tom St Denis
- Re: Casting the return value of malloc()...
- From: gwowen
- Re: Casting the return value of malloc()...
- From: Richard Bos
- Re: Casting the return value of malloc()...
- From: gwowen
- Re: Casting the return value of malloc()...
- From: Richard Harter
- Re: Casting the return value of malloc()...
- From: Joe Wright
- Re: Casting the return value of malloc()...
- From: Richard Harter
- Re: Casting the return value of malloc()...
- From: Richard Heathfield
- Re: Casting the return value of malloc()...
- From: Richard Harter
- Re: Casting the return value of malloc()...
- From: Keith Thompson
- Re: Casting the return value of malloc()...
- From: Richard Harter
- Common misconceptions about C (C95)
- Prev by Date: Re: Common misconceptions about C (C95)
- Next by Date: Re: Casting the return value of malloc()...
- Previous by thread: Re: Casting the return value of malloc()...
- Next by thread: Re: Casting the return value of malloc()...
- Index(es):
Relevant Pages
|