Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #65979

Re: Allocating memory styles

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Allocating memory styles
Date 2015-07-20 14:18 +0100
Organization A noiseless patient Spider
Message-ID <87mvyr6k83.fsf@bsb.me.uk> (permalink)
References <0591b244-c066-4714-afad-3ba8db4a8676@googlegroups.com>

Show all headers | View raw


janus <emekamicro@gmail.com> writes:

> I am going through a book and I stumbled on this class of memory allocation:
>
> void* p = calloc(1, class->size);

I'm going to assume that should be P rather than p.  Everywhere else you
talk about P.

> *(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.

You are confusing structs and pointers all over the place here.  It's
not helped by the fact that you are being careless about case.  The book
uses "Class" for a struct tag and "class" for pointer to something
(probably struct Class but you don't show us that part) so you need to
be very careful how you write.

Anyway, I think you'll find that P points to structure whose first
element is a pointer to a struct Class.

Writing this:

  *(T *)ptr = x;

is a way to pretend that ptr points to an object of type T.  (If you had
a pointer of the right type already you could just write *ptr = x; but
your book is playing games with the types in order to build an object
system in C.)

Your line has exactly this form but T just happens to be a pointer type:

  *((struct Class *)  *)P = class;

I.e. pretend that P points to a pointer to a struct Class and set that
struct Class pointer to the value of "class"

> But why using
> pointer-to-pointer?

You've just lost a level of indirection along the way.  The first
element in the thing pointed to by P is, in fact, a pointer.

> I was thinking that the same thing might be achieved by this:
>
> (const struct Class*) P = class;

That fails for a couple of reasons, not least that you can't assign to
the result of cast.  The posh wording for this is that a cast expression
is not an lvalue.

(If I've guessed wrong, please post all the relevant declarations.)

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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