Re: Casting the return value of malloc()...



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"
.



Relevant Pages

  • Re: C the complete nonsense
    ... he correctly parenthesizes the formal parameters in the macro, ... Very few C programmers in practice take the next step, ... curly brackets and expression macros in round parentheses. ...
    (comp.lang.c)
  • Re: C99: Suggestions for style(9)
    ... because it does not improve maintainability in any way. ... There is no source for confusion here, so the rule even contradicts the rule, which states not to use redundant parentheses. ... so it is clear that there is a macro at work. ... the parentheses are not redundant) would be for local debugging ...
    (freebsd-hackers)
  • Re: Casting the return value of malloc()...
    ... If the operand is a macro, and the macro is defined in such a way that ... parentheses are necessary, *fix the macro definition*. ... Idiom is more robust. ...
    (comp.lang.c)
  • Re: Compare to a defined constants in C?
    ... int main ... doesn't need a semicolon to terminate it; ... Another thing to watch out for is that a macro that's to be used ... Judicious use of parentheses can avoid most problems. ...
    (comp.lang.c)