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


Groups > comp.lang.c > #26821

Re: (newbie) question concerning memory allocation

Date 2012-09-29 19:18 +0100
From lipska the kat <lipskathekat@yahoo.co.uk>
Newsgroups comp.lang.c
Subject Re: (newbie) question concerning memory allocation
References <zZKdnQK8KpXJSPvNnZ2dnUVZ8u6dnZ2d@bt.com> <k46rip$v8g$1@dont-email.me>
Message-ID <-KOdnVXFPIH_pvrNnZ2dnUVZ7s-dnZ2d@bt.com> (permalink)

Show all headers | View raw


On 29/09/12 14:03, Eric Sosman wrote:
> On 9/29/2012 7:02 AM, lipska the kat wrote:
>> [...]
[snip]

> Either way, your
> program is not just living on the edge, it's fallen over.

:-( ah well, he who never made a mistake never made anything.
At least I think that's how it goes.

[snip]

>
> It looks like you wanted to initialize `foos' to point at the
> beginning of an array of `x' (or more) memory locations that each
> can hold a `struct foo*':
>
> foos -> [0] -> first malloc() result
> [1] -> second malloc() result
> ...
> [x-1] -> final malloc() result

What I was actually trying to do was to come up with some way
to allow the addition of new instances of foo at runtime without
having to know how many I would eventually need. What I actually need is 
a linked list, this is trivial to implement and I don't know why I 
didn't just go for that ... trying new things hoping to discover a 
better way of doing things I suppose, anyway ...

> One way to do this would be
>
> foos = malloc(x * sizeof(struct foo*));
>
> or equivalently (and usually better)
>
> foos = malloc(x * sizeof *foos);

These methods all appear to require knowing the value of x at compile 
time. I noticed that

int main(int argc, char *argv[])

could also be written

int main(int argc, char **argv)

I suppose I was trying to reproduce this for pointers to struct foo

> But you neglected to do any such thing, and that's probably the
> root of your trouble.

Probably, now that you point it out.

Thanks for taking the time to reply, it's much appreciated.

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

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