Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #150658
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: allocating two objects in one malloc |
| Date | 2020-03-18 14:21 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <r4torg$evs$1@dont-email.me> (permalink) |
| References | <20301314-7f32-4cab-924d-c55c7a5175d6@googlegroups.com> <473akg-l3a2.ln1@mail.home.palmen-it.de> |
On 3/18/20 1:43 PM, Felix Palmen wrote:
> * Thiago Adams <thiago.adams@gmail.com>:
>> I want to allocate an object array of some type using malloc and
>> allocate together two integers.(int or size_t)
>
> That's what structs are for.
>
>> int n = 100;
>> int *p = (int*) malloc(sizeof(int) * 2 + sizeof(char) * n);
>
> Just: why? This is unreadable and error-prone. And using this construct
> as you intend to is aliasing -- in this special case allowable, because
> `char` may alias anything else, but still a code smell, if it's done
> without a compelling reason. An allocated object gets its effective type
> on the first write to it, so the instant you write an integer to p[0], p
> is a pointer to int.
'p' is always a pointer to int, whether or not it has ever been used in
an expression.
Something like that would be acceptable even if char wasn't one of the
two types in question. The expression p[0] does not access the entire
object allocated by that call to malloc(), it only accesses a single int
sub-object at the start of that block of memory. Therefore, it's only
that sub-object which acquires an effective type of 'int'. You can
therefore, if you take care to avoid overlap and ensure correct
alignment, give different effective types to different parts of a single
allocation without running afoul of the anti-aliasing rules.
A lot of software depends upon that. For instance, I once had to figure
out in detail how the IMSL library allocated multidimensional objects.
The header for such an object was a struct type containing a integer
rank for the array, a pointer to an array of integers of that length
containing the dimensions of that array, and a pointer to the actual
floating point data contained in the array. Those things were not
separately allocated. The IMSL library allocated a single block of
memory sufficiently large to hold the header, the dimensions array, and
the data. It placed the header at the start of the block, then
constructed the dimensions array immediately after it, and constructed
the data array immediately after that, filling in the header with
pointers to those items, then returned a pointer to the header. The
entire data structure would therefore be free()d by a single call to
free() using that pointer. I seem to recall that there were some
additional complications, but I no longer remember the details - the
part I did remember is sufficient to make my point.
In C99, the header could have contained the dimensions array as a
flexible array member, but that version of the IMSL library predated C99.
> Furthermore, casting the result of malloc is a C++-ism.
Agreed. The cast is unnecessary in C. If, by any chance, your code needs
to be compatible with C90 or a pre-standard compiler, the cast can even
hide an error in your code.
> A sane version for your allocation could look like this:
>
> struct fooarray
> {
> int num[2];
> char content[];
> };
> [...]
>
> int n = 100;
> struct fooarray *p = malloc(sizeof *p + n);
I certainly agree that this is a better approach. But the other way,
despite its faults, does have well-defined behavior.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:14 -0700
Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 18:17 +0100
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:55 -0700
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 11:01 -0700
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:14 +0100
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 11:35 -0700
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:44 +0100
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 12:07 -0700
Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-18 20:39 +0000
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 13:52 -0700
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 23:11 +0100
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 16:48 -0700
Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-19 01:16 +0000
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 06:11 -0700
Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 14:49 +0000
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:46 -0700
Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 07:50 -0700
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:48 -0700
Re: allocating two objects in one malloc chad <cdalten@gmail.com> - 2020-03-19 05:15 -0700
Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 08:11 -0700
Re: allocating two objects in one malloc Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-24 11:35 -0400
Re: allocating two objects in one malloc Richard Damon <Richard@Damon-Family.org> - 2020-03-24 13:50 -0400
Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 20:58 +0000
Re: allocating two objects in one malloc Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 13:10 -0700
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:52 -0700
Re: allocating two objects in one malloc Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 13:05 -0700
Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:53 +0100
Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-18 20:29 +0000
Re: allocating two objects in one malloc jadill33@gmail.com - 2020-03-18 13:44 -0700
Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-18 21:00 +0000
Re: allocating two objects in one malloc Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-18 10:18 -0700
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 18:43 +0100
Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 18:55 +0100
Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:56 -0700
Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:06 +0100
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:01 +0100
Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 14:21 -0400
Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:30 +0100
Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 15:08 -0400
csiph-web