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


Groups > comp.lang.c > #27151

Re: (newbie) question concerning memory allocation

Newsgroups comp.lang.c
Date 2012-10-10 15:15 -0700
References <zZKdnQK8KpXJSPvNnZ2dnUVZ8u6dnZ2d@bt.com> <0.d6fa1cebebeeb5fa249a.20120929141156BST.87wqzd55yb.fsf@bsb.me.uk>
Message-ID <f42bef0a-e72f-4f85-9f94-809873fa3641@o7g2000yqb.googlegroups.com> (permalink)
Subject Re: (newbie) question concerning memory allocation
From Anand Hariharan <mailto.anand.hariharan@gmail.com>

Show all headers | View raw


On Sep 29, 8:11 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> lipska the kat <lipskathe...@yahoo.co.uk> writes:
(...)
> >    /* create foos*/
> >    for(i = 0; i < x; i++){
> >            *foos = malloc(sizeof(struct foo));
>
> Form what you write below, you know what's wrong here, yes?
>

Considering that the OP had difficulties with the dereferencing
syntax, in addition to what you say below ...


> I like to use this pattern for malloc calls: p = malloc(n * sizeof *p);
> so that there is no need to check the you are using the right type in
> the sizeof expression.  When the number is obviously 1, I omit it.:
>
>   *foos = malloc(sizeof **foos);
>

... I would elaborate that the sizeof expression is evaluated at
compile time and there isn't any dereferencing involved.

Writing malloc like above is idiomatic usage and good from a software
engineering perspective; but I find that it is quite difficult to
explain it to someone learning C.  YMMV.

- Anand

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


Thread

(newbie) question concerning memory allocation lipska the kat <lipskathekat@yahoo.co.uk> - 2012-09-29 12:02 +0100
  Re: (newbie) question concerning memory allocation Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-29 09:03 -0400
    Re: (newbie) question concerning memory allocation lipska the kat <lipskathekat@yahoo.co.uk> - 2012-09-29 19:18 +0100
      Re: (newbie) question concerning memory allocation Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-29 14:52 -0400
      Re: (newbie) question concerning memory allocation Barry Schwarz <schwarzb@dqel.com> - 2012-09-29 11:56 -0700
        Re: (newbie) question concerning memory allocation lipska the kat <lipskathekat@yahoo.co.uk> - 2012-09-29 20:34 +0100
          Re: (newbie) question concerning memory allocation Barry Schwarz <schwarzb@dqel.com> - 2012-09-29 14:28 -0700
            Re: (newbie) question concerning memory allocation Keith Thompson <kst-u@mib.org> - 2012-09-29 15:40 -0700
      Re: (newbie) question concerning memory allocation Paul N <gw7rib@aol.com> - 2012-09-30 12:21 -0700
        Re: (newbie) question concerning memory allocation lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-01 08:35 +0100
          Re: (newbie) question concerning memory allocation Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-10 23:38 +0300
  Re: (newbie) question concerning memory allocation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-29 14:11 +0100
    Re: (newbie) question concerning memory allocation lipska the kat <lipskathekat@yahoo.co.uk> - 2012-09-29 19:47 +0100
      Re: (newbie) question concerning memory allocation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-29 20:47 +0100
        Re: (newbie) question concerning memory allocation Keith Thompson <kst-u@mib.org> - 2012-09-29 14:43 -0700
    Re: (newbie) question concerning memory allocation Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-10-10 15:15 -0700
  Re: (newbie) question concerning memory allocation Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-09-29 15:48 -0600

csiph-web