Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Allocating memory styles |
| Date | 2015-07-20 13:47 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <d14cg3Faap9U1@mid.uni-berlin.de> (permalink) |
| References | <0591b244-c066-4714-afad-3ba8db4a8676@googlegroups.com> |
janus <emekamicro@gmail.com> wrote:
> I am going through a book and I stumbled on this class of memory allocation:
> void* p = calloc(1, class->size);
> *(const struct Class**) P = class;
> The last line is what is hard for me to understand. Why casting to pointer-to-pointer, and then dereference it immediately? My understanding is that struct Class is the first element in P struct, so by casting P struct by struct class, one immediately has access to the first element of the struct. This makes sense to me. But why using pointer-to-pointer?
> I was thinking that the same thing might be achieved by this:
> (const struct Class*) P = class;
No, with that you'd have an rvalue on the left hand side -
the result of a cast is a value, the value of what's stored
in 'p', but with a different type attached to it, and not a
(new) variable that you can assign to (try it out and see
what the compiler is going to tell you).
But the original code is rather strange. 'class' obviously
must be a pointer (to some kind of structure), otherwise
'class->size' would be a syntax errer. And 'p' seems to be
intended to be used as another pointer to such a structure,
or the second line makes no sense. But does the expression
'class->size' really is the size of such a pointer? Without
looking at the code I'd be rather worried it isn't. And I'd
definitely write it differently to avoid such worries, i.e.
by using
void * p = malloc( sizeof class );
(the use of calloc() also makes no sense here and zero-
ing elements that way only works reliably for integers
anyway).
Then the value of 'class' is copied over to 'p', making
them both point to the same structure.
Without any more information to go by my gut-feeling
is that someone actually wanted to make a copy of the
structure 'class' points to, but messed up. In that
case 'slass->size' would be the size of such a struc-
ture (but which still could have been written more
clearly as 'sizeof *class') but then the assignment to
'p' from 'class' is wrong, it would have to be
* ( struct Class const * ) p = *class;
(which works because you can copy structures by value).
Or did you make a mistake when copying the code (as you
rather likely did when mixing up 'p' and 'P' - that's
two different identifiers)?
The next question, of course, is why making 'p' a void
pointer instead of what it's intended use is? Using the
proper type would make the ugly cast unnecessary...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Allocating memory styles janus <emekamicro@gmail.com> - 2015-07-20 05:36 -0700
Re: Allocating memory styles Noob <root@127.0.0.1> - 2015-07-20 14:53 +0200
Re: Allocating memory styles janus <emekamicro@gmail.com> - 2015-07-20 07:23 -0700
Re: Allocating memory styles Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-20 16:40 +0100
Re: Allocating memory styles janus <emekamicro@gmail.com> - 2015-07-20 12:19 -0700
Re: Allocating memory styles Richard Heathfield <rjh@cpax.org.uk> - 2015-07-20 14:07 +0100
Re: Allocating memory styles Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-20 14:18 +0100
Re: Allocating memory styles jt@toerring.de (Jens Thoms Toerring) - 2015-07-20 13:47 +0000
Re: Allocating memory styles Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-20 15:21 +0100
Re: Allocating memory styles Johann Klammer <klammerj@NOSPAM.a1.net> - 2015-07-20 16:27 +0200
Re: Allocating memory styles luser droog <luser.droog@gmail.com> - 2015-07-20 12:34 -0700
Re: Allocating memory styles Keith Thompson <kst-u@mib.org> - 2015-07-20 14:00 -0700
Re: Allocating memory styles luser droog <luser.droog@gmail.com> - 2015-07-20 20:29 -0700
Re: Allocating memory styles raltbos@xs4all.nl (Richard Bos) - 2015-07-22 15:21 +0000
Re: Allocating memory styles janus <emekamicro@gmail.com> - 2015-07-20 14:13 -0700
Re: Allocating memory styles "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-20 14:54 -0700
Re: Allocating memory styles "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-20 14:57 -0700
csiph-web