Initializing C array of objects

  • Hi

    I'm writing a class that needs to allocate a C array of objects and
    am having some trouble getting the things initialized. Here's what
    I'm doing but it's giving me "lvalue" errors

    Foo        *fooArray        = malloc(10 * sizeof(Foo)),
      *fooObject;

    int        i;

    for (i = 0; i < 10; i++)
    {
    (fooArray + i) = [foo init];
    }

    Also tried allocating as array of Foo pointers like so

    Foo        *fooArray        = malloc(10 * sizeof(Foo *)),
      *fooObject;

    int        i;

    for (i = 0; i < 10; i++)
    {
    fooObject = (fooArray + i);

    // in debugger, this shows that a new object is allocated rather
    than putting it into the exsting aray
    fooObject  = [[Foo alloc] init];
    }

    How does one do something like this?

    Thanks for any help;

    Ken
  • Le 20 déc. 07 à 11:58, Ken Tozier a écrit :

    > Hi
    >
    > I'm writing a class that needs to allocate a C array of objects and
    > am having some trouble getting the things initialized. Here's what
    > I'm doing but it's giving me "lvalue" errors
    >
    > Foo        *fooArray        = malloc(10 * sizeof(Foo)),
    > *fooObject;
    >
    > for (i = 0; i < 10; i++)
    > {
    > fooObject    = (fooArray + i);
    >
    > // in debugger, this shows that a new object is allocated rather
    > than putting it into the exsting aray
    > fooObject     = [[Foo alloc] init];
    > }
    >
    > How does one do something like this?
    >
    > Thanks for any help;
    >
    > Ken

    Object in Obj-c cannot be referenced directly. You have to store
    object pointer.
    [[Foo alloc] init] return type is 'id' (a pointer to an object)

    Foo **fooArray = malloc(10 * sizeof(Foo *));
    int i;
    for (i = 0; i < 10; i++)
    {
    fooObject[i] = [[Foo alloc] init];
    }
  • > Hi
    >
    > I'm writing a class that needs to allocate a C array of objects and
    > am having some trouble getting the things initialized. Here's what
    > I'm doing but it's giving me "lvalue" errors
    >
    > Foo        *fooArray        = malloc(10 * sizeof(Foo)),
    > *fooObject;
    >
    > int        i;
    >
    > for (i = 0; i < 10; i++)
    > {
    > (fooArray + i) = [foo init];
    > }
    >

    I think you should use :

    Foo **fooArray = malloc(10 * sizeof(Foo *));

    for (int i = 0; 1 < 10; i++)
    fooArray[i] = [[Foo alloc] init];

    But, why not a NSArray?

    :: marcelo.alves
  • On 20 Dec 07, at 02:58, Ken Tozier wrote:
    > I'm writing a class that needs to allocate a C array of objects and
    > am having some trouble getting the things initialized. Here's what
    > I'm doing but it's giving me "lvalue" errors
    <snip>
    > (fooArray + i) = [foo init];

    I'm not sure what exactly you're trying to accomplish here, but you
    can't assign a value ([foo init]) to the result of a calculation
    (fooArray + i). But that's kind of tangential to the actual problem...

    > How does one do something like this?

    You can't. The Objective-C runtime depends on being able to manage
    memory for each object individually. In particular*, [NSObject alloc]
    calls malloc, and [NSObject dealloc] calls free(self) as its last
    action - trying to do this on a memory region that wasn't returned by
    malloc() will fail horribly.

    *: This isn't necessarily exactly what the runtime is doing, but it's
    a reasonable facsimile. You shouldn't depend on these exact details
    being true, but you can (and should) take home the lesson that you
    can't do Cocoa's memory management for it.

    Unless you're dealing with a very large number of very small objects,
    you're probably better off just using NSArray. The overhead is lower
    than you might imagine. If you *are* dealing with a lot of very small
    objects, or for some other reason NSArray isn't appropriate, you're
    just going to have to bite the bullet and store a bunch of structs
    instead.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today