Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: just curious:Equel |
| Date | 2015-09-09 10:22 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <kfnlhcfld75.fsf@x-alumni2.alumni.caltech.edu> (permalink) |
| References | (8 earlier) <p-20150902215300@ram.dialup.fu-berlin.de> <kfnh9n7tj7y.fsf@x-alumni2.alumni.caltech.edu> <msjjha$sgr$1@speranza.aioe.org> <kfnfv2pmocv.fsf@x-alumni2.alumni.caltech.edu> <msm6jq$qj8$1@speranza.aioe.org> |
August Karlstrom <fusionfile@gmail.com> writes:
> On 2015-09-08 08:12, Tim Rentsch wrote:
>> August Karlstrom <fusionfile@gmail.com> writes:
>>> Easier is just NEW(p) after having defined NEW as
>>>
>>> #define NEW(ptr) (ptr) = malloc(sizeof *(ptr))
>>
>> I sometimes use this technique, mainly for better factoring.
>
> Yes, it makes it easier to switch to a different allocator, like for
> instance GC_MALLOC.
>
>> Also a two-argument form that allows allocating an array.
>
> Indeed. These are the three function macros I always use in my projects:
>
> #include <stdlib.h>
>
> #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)
I agree on the macros, although I would advocate different
choices for the names. I've settled on LIMIT() for what you call
LEN(). (As a rule I avoided shortened words like "len" and "ptr"
- if a word is meant, better to use the whole word.) For the
NEW's, my naming heuristics say the more general form should
have the shorter name, so maybe NEW(p,n) and NEW1(p) (or perhaps
MALLOC(p) for the second one?). For what that's worth.
Incidentally, you probably know this, but these NEW's can be used
as parts of declarations:
int *NEW( stuff, 20 );
I expect some people would frown on that kind of usage. I agree
that it might seem a little weird, but not so much as to not use
it; after all, the similarity of initializing declarations and
assigments is not accidental, so even if the idiom is somewhat
surprising it shouldn't be overly so.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
just curious:Equel G G <gdotone@gmail.com> - 2015-09-01 22:37 -0700
Re: just curious:Equel James Kuyper <jameskuyper@verizon.net> - 2015-09-02 09:56 -0400
Re: just curious:Equel Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-02 16:03 +0100
Re: just curious:Equel "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-02 08:12 -0700
Re: just curious:Equel gordonb.d8f7l@burditt.org (Gordon Burditt) - 2015-09-02 11:09 -0500
Re: just curious:Equel Richard Heathfield <rjh@cpax.org.uk> - 2015-09-02 17:27 +0100
Re: just curious:Equel "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-02 09:36 -0700
Re: just curious:Equel Richard Heathfield <rjh@cpax.org.uk> - 2015-09-02 17:57 +0100
Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-02 22:34 +0200
Re: just curious:Equel Richard Heathfield <rjh@cpax.org.uk> - 2015-09-02 21:48 +0100
Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-03 09:31 +0200
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:29 -0700
Re: just curious:Equel James Kuyper <jameskuyper@verizon.net> - 2015-09-02 17:09 -0400
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:33 -0700
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 12:56 -0700
Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-07 10:57 +0200
Re: just curious:Equel Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-09-07 19:29 +0000
Re: just curious:Equel James Kuyper <jameskuyper@verizon.net> - 2015-09-07 15:49 -0400
Re: just curious:Equel Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-09-10 16:44 +0000
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-07 23:12 -0700
Re: just curious:Equel August Karlstrom <fusionfile@gmail.com> - 2015-09-08 10:35 +0200
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
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:25 -0700
Re: just curious:Equel Ike Naar <ike@iceland.freeshell.org> - 2015-09-07 09:14 +0000
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 15:44 -0700
Re: just curious:Equel Keith Thompson <kst-u@mib.org> - 2015-09-06 17:27 -0700
Re: just curious:Equel Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-08 10:49 -0700
Re: just curious:Equel James Kuyper <jameskuyper@verizon.net> - 2015-09-02 11:49 -0400
Re: just curious:Equel Jay Braun <lyngwyst@gmail.com> - 2015-09-02 13:02 -0700
Re: just curious:Equel "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-02 13:08 -0700
Re: just curious:Equel G G <gdotone@gmail.com> - 2015-09-02 16:10 -0700
Re: just curious:Equel Bartc <bc@freeuk.com> - 2015-09-02 21:30 +0100
Re: just curious:Equel "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-02 13:37 -0700
Re: just curious:Equel Jay Braun <lyngwyst@gmail.com> - 2015-09-02 13:44 -0700
Re: just curious:Equel "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-02 13:52 -0700
Re: just curious:Equel G G <gdotone@gmail.com> - 2015-09-02 16:07 -0700
Re: just curious:Equel Andreas Perstinger <andipersti@gmail.com> - 2015-09-03 08:49 +0000
Re: just curious:Equel Bartc <bc@freeuk.com> - 2015-09-03 10:47 +0100
Re: just curious:Equel G G <gdotone@gmail.com> - 2015-09-02 16:17 -0700
csiph-web