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


Groups > comp.lang.c > #65976

Re: Allocating memory styles

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Allocating memory styles
Date 2015-07-20 14:07 +0100
Organization go, ion artisan!
Message-ID <moiroh$895$1@dont-email.me> (permalink)
References <0591b244-c066-4714-afad-3ba8db4a8676@googlegroups.com>

Show all headers | View raw


On 20/07/15 13:36, 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;

My first question would be: by P, do you mean p? (Or, conversely, by p, 
do you mean P?) C is case-sensitive.

My best guess is that what he meant to write is:

void *p = calloc(1, class->size);
if(p != NULL)
{
   memcpy(p, class, class->size);
}
else
{
   handle the lack of memory in some sensible way
}

If in doubt, don't cast. If in doubt and still tempted to cast, still 
don't cast. If still in doubt and still tempted to cast, ask comp.lang.c 
to show you how to achieve your objective in a better way.

There are a very few problems for which casting is the right solution, 
but they are extraordinarily few and terribly far between. The above is 
not one of those problems.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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