Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Allocating memory styles |
| Date | 2015-07-20 15:21 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87egk27vup.fsf@bsb.me.uk> (permalink) |
| References | <0591b244-c066-4714-afad-3ba8db4a8676@googlegroups.com> <d14cg3Faap9U1@mid.uni-berlin.de> |
jt@toerring.de (Jens Thoms Toerring) writes: > 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. My guess: P is supposed to point to an "object" of the class described by the structure pointed to by the class variable. The very first member of such an object is a pointer to it's class description. That's my guess at least. I've seen that sort of code about dozen times because there was a time when every one write their own object system in C. <snip> -- Ben.
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