home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:18291 comp.std.c:3196
- Path: sparky!uunet!mcsun!Germany.EU.net!mikros!mwtech!martin
- From: martin@mwtech.UUCP (Martin Weitzel)
- Newsgroups: comp.lang.c,comp.std.c
- Subject: Re: Is this ANSI: final (long)
- Message-ID: <1363@mwtech.UUCP>
- Date: 13 Dec 92 12:01:33 GMT
- References: <Zc15uB4w165w@quest.UUCP> <723773123.1323@hitech.com.au> <alien.01ss@acheron.amigans.gen.nz> <Bz3n1I.73o@unx.sas.com>
- Reply-To: martin@mwtech.UUCP (Martin Weitzel)
- Organization: MIKROS Systemware, Darmstadt/W-Germany
- Lines: 36
-
- In article <Bz3n1I.73o@unx.sas.com> sasghm@theseus.unx.sas.com (Gary Merrill) writes:
- :
- :In article <alien.01ss@acheron.amigans.gen.nz>, alien@acheron.amigans.gen.nz (Ross Smith) writes:
- :
- :|>
- :|> I've seen this idiom before, but I've never seen the point of it. Why not just
- :|> use a pointer instead of kludging it up with a fake array?
- :|>
- :|> struct FM
- :|> {
- :|> int data_count;
- :|> char *data; /* Again, might be int for alignment */
- :|> }
- :|> thing;
- :|>
- :|> ...
- :|>
- :|> thing.data = malloc(extra_data_size);
- :|> thing.data_count = extra_data_size;
- :
- :The point is this: If you are dynamically allocating *both* the
- :structure and the data, the approach you suggest requires *two*
- :separate allocations (and two separate frees if you will be freeing
- :the stuff). The goal is to reduce the calls to the memory management
- :functions. Doing so can result in easily measureable performance
- :improvements. Likewise, you have the extra overhead of setting
- :the pointer to the new memory, but this isn't much. So to reduce
- :the call overhead you *could* allocate a big chunk and use the first
- :part of it for the structure and the rest for the data.
-
- Yes, and there's another point: With a single allocation you may
- memcpy/memmove/memcmp/[f]read/[f]write the whole easily. Each of
- this would become a specialized operation if you separate the size
- from the data.
- --
- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83
-