Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Johann Klammer <klammerj@NOSPAM.a1.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Allocating memory styles |
| Date | 2015-07-20 16:27 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <moj0ge$hea$2@speranza.aioe.org> (permalink) |
| References | <0591b244-c066-4714-afad-3ba8db4a8676@googlegroups.com> |
On 07/20/2015 02:36 PM, janus wrote: > Hello All, > > > 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; Regards, \Emeka > can you show the whole snippet? I think something is missing... There are special ways of initializing structs(I believe libfreetype uses such)... might be related to this... > error = FT_New_Face( library, "/usr/share/fonts/truetype/arial.ttf", > 0, &face ); They have face be a pointer type, then &face is a pointer to a pointer. So they can set *face=NULL inside the function, if stuff goes wrong. Would not work by-value. (It would 'work' as face=NULL, but not visible outside).
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