Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #71183

Re: just curious:Equel

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: just curious:Equel
Date 2015-09-24 19:56 +0100
Organization A noiseless patient Spider
Message-ID <87k2rffxy1.fsf@bsb.me.uk> (permalink)
References (14 earlier) <msrehd$d8j$1@speranza.aioe.org> <MPG.306d279abfa29e7863@news.eternal-september.org> <mu0b1k$vf1$1@speranza.aioe.org> <87vbb0f9w4.fsf@bsb.me.uk> <MPG.306dd2a3c60d906566@news.eternal-september.org>

Show all headers | View raw


Philip Lantz <prl@canterey.us> writes:

> Ben Bacarisse wrote:
>> 
>> August Karlstrom <fusionfile@gmail.com> writes:
>> 
>> > On 2015-09-24 07:18, Philip Lantz wrote:
>> >> On 2015-09-10, August Karlstrom wrote:
>> >>> On 2015-09-09 19:22, Tim Rentsch wrote:
>> >>>> August Karlstrom <fusionfile@gmail.com> writes:
>> >>>>> #define LEN(arr) (sizeof (arr) / sizeof (arr)[0])
>> >>>>>
>> >>>>> #define NEW_ARRAY(ptr, n) (ptr) = malloc((n) * sizeof *(ptr))
>> >>>>>
>> >>>>> #define NEW(ptr) NEW_ARRAY((ptr), 1)
>> >>>>
>> >>>> Incidentally, you probably know this, but these NEW's can be used as
>> >>>> parts of declarations:
>> >>>>
>> >>>> int *NEW( stuff, 20 );
>> >>>
>> >>> Or "misused", depending on how you see it. ;-)
>> >>
>> >> Perhaps Tim was slyly pointing out that you omitted the usual
>> >> parentheses around the macro expansion.
>> >>
>> >> Oh, I see from his next response that he probably wasn't. But fully
>> >> parenthesizing macros that expand to expressions is generally considered
>> >> good form, and it would prevent the "misuse", should you care to do so.
>> >
>> > I'm thinking that the macro NEW is semantically a function call
>> > *statement*. Therefor it's not meant to be used  in expressions (or in
>> > declarations like the one above).
>> 
>> If you want, you can enforce that rule by writing the macro like this:
>> 
>> #define NEW_ARRAY(ptr, n) do (ptr) = malloc((n) * sizeof *(ptr)); while (0)
>
> Or
> #define NEW_ARRAY(ptr, n) ((void)((ptr) = malloc((n) * sizeof *(ptr))))
> which would be my preference.

Sure, though that does not, technically, stop it being using in
expressions.  I don't mean that it can be used as the sole expression in
an expression statement -- that's obviously the purpose -- but it can
also be used in a comma expression or, more oddly, in a conditional
expression.  I don't think this matters much.  In fact both such uses
could be considered "statement-like" and therefore perfectly acceptable
even to someone who wants to exclude the macro from expression.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-09 10:22 -0700
  Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-10 10:21 +0200
    Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-11 12:06 -0700
      Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-12 17:53 +0200
        Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 16:04 -0700
          Re: just curious:Equel Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-26 16:17 -0700
            Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 12:53 -0700
          Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-28 18:00 +0200
            Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-01 12:51 -0700
    Re: just curious:Equel Philip Lantz <prl@canterey.us> - 2015-09-23 22:18 -0700
      Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-24 10:08 +0200
        Re: just curious:Equel Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-24 10:23 +0100
          Re: just curious:Equel Philip Lantz <prl@canterey.us> - 2015-09-24 10:28 -0700
            Re: just curious:Equel Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-24 19:56 +0100
        Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 16:16 -0700
          Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-28 08:18 +0200
      Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 16:10 -0700
        Re: just curious:Equel supercat@casperkitty.com - 2015-09-27 11:40 -0700
          Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-29 10:03 -0700

csiph-web