www.digitalmars.com         C & C++   DMDScript  

D - Arrays of Class References

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
So let's says that you want to allocate a whole bunch of class objects
and have an array of them.  The only method I can come up with is this
Java-esque nightmare:

class Foo {...}

int main()
{
    Foo objs[];
    ...
    for(int i=0; i<objs.length; i++)
        objs[i] = new Foo;
    ...
}



Isn't there some way to create a whole lot of class objects at once and
build an array of references to them?

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
May 03 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CD300C7.2814DFC4 deming-os.org...
 So let's says that you want to allocate a whole bunch of class objects
 and have an array of them.  The only method I can come up with is this
 Java-esque nightmare:

 class Foo {...}
 int main()
 {
     Foo objs[];
     ...
     for(int i=0; i<objs.length; i++)
         objs[i] = new Foo;
     ...
 }
 Isn't there some way to create a whole lot of class objects at once and
 build an array of references to them?

Why is it a nightmare?
May 03 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Walter wrote:

 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CD300C7.2814DFC4 deming-os.org...
 So let's says that you want to allocate a whole bunch of class objects
 and have an array of them.  The only method I can come up with is this
 Java-esque nightmare:

 class Foo {...}
 int main()
 {
     Foo objs[];
     ...
     for(int i=0; i<objs.length; i++)
         objs[i] = new Foo;
     ...
 }
 Isn't there some way to create a whole lot of class objects at once and
 build an array of references to them?

Why is it a nightmare?

It separates the algorithm from the intent. The intent is, "create an array of Foo's". The algorithm is something very different. Also, it exposed the problem that (as I understand it) each Foo object is a separate object in the garbage collector. Having per-object collection is usually nice, but in some cases it doesn't make sense. In some programs, it would be nice to be able to allocate a large block of objects and have them count as a single entity in the garbage collector. -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
May 04 2002
next sibling parent reply "Walter" <walter digitalmars.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CD387A9.5778D3E5 deming-os.org...
 Walter wrote:

 Why is it a nightmare?


 of Foo's".  The algorithm is something very different.

I use loops like that so often, they in my mind are one semantic entity anyway <g>.
 Also, it exposed the problem that (as I understand it) each Foo object is

 separate object in the garbage collector.  Having per-object collection is
 usually nice, but in some cases it doesn't make sense.  In some programs,

 would be nice to be able to allocate a large block of objects and have

 count as a single entity in the garbage collector.

It's possible to do that in D if you're willing to step outside of convention.
May 05 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:ab3p3b$2p7m$1 digitaldaemon.com...

 I use loops like that so often, they in my mind are one semantic entity
 anyway <g>.

Oh yes, I remember your reply to suggestions of for-each loop... =)
May 05 2002
prev sibling parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CD387A9.5778D3E5 deming-os.org...
 Walter wrote:

 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CD300C7.2814DFC4 deming-os.org...
 So let's says that you want to allocate a whole bunch of class objects
 and have an array of them.  The only method I can come up with is this
 Java-esque nightmare:

 class Foo {...}
 int main()
 {
     Foo objs[];
     ...
     for(int i=0; i<objs.length; i++)
         objs[i] = new Foo;
     ...
 }
 Isn't there some way to create a whole lot of class objects at once



 build an array of references to them?

Why is it a nightmare?

It separates the algorithm from the intent. The intent is, "create an

 of Foo's".  The algorithm is something very different.

 Also, it exposed the problem that (as I understand it) each Foo object is

 separate object in the garbage collector.  Having per-object collection is
 usually nice, but in some cases it doesn't make sense.  In some programs,

 would be nice to be able to allocate a large block of objects and have

 count as a single entity in the garbage collector.

 --
 The Villagers are Online! http://villagersonline.com

 .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
 .[ (a version.of(English).(precise.more)) is(possible) ]
 ?[ you want.to(help(develop(it))) ]

Use a collection class: class Foo {...} class FooCollection { this (int iFooCount) { objs.length = iFooCount; for(int i=0; i<objs.length; i++) objs[i] = new Foo; } Foo objs[]; } int main() { FooCollection fcFoos = new FooCollection (100); // ... return 0; } This way you can allocate a whole bunch of Foo's in one swoop. Also the Foo's will act as a single entity to the garbage collector because they are all contained in the same FooCollection. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
May 05 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
OddesE wrote:

 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CD387A9.5778D3E5 deming-os.org...
 Walter wrote:

 "Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
 news:3CD300C7.2814DFC4 deming-os.org...
 So let's says that you want to allocate a whole bunch of class objects
 and have an array of them.  The only method I can come up with is this
 Java-esque nightmare:

 class Foo {...}
 int main()
 {
     Foo objs[];
     ...
     for(int i=0; i<objs.length; i++)
         objs[i] = new Foo;
     ...
 }
 Isn't there some way to create a whole lot of class objects at once



 build an array of references to them?

Why is it a nightmare?

It separates the algorithm from the intent. The intent is, "create an

 of Foo's".  The algorithm is something very different.

 Also, it exposed the problem that (as I understand it) each Foo object is

 separate object in the garbage collector.  Having per-object collection is
 usually nice, but in some cases it doesn't make sense.  In some programs,

 would be nice to be able to allocate a large block of objects and have

 count as a single entity in the garbage collector.

 --
 The Villagers are Online! http://villagersonline.com

 .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
 .[ (a version.of(English).(precise.more)) is(possible) ]
 ?[ you want.to(help(develop(it))) ]

Use a collection class: class Foo {...} class FooCollection { this (int iFooCount) { objs.length = iFooCount; for(int i=0; i<objs.length; i++) objs[i] = new Foo; } Foo objs[]; } int main() { FooCollection fcFoos = new FooCollection (100); // ... return 0; } This way you can allocate a whole bunch of Foo's in one swoop. Also the Foo's will act as a single entity to the garbage collector because they are all contained in the same FooCollection.

They might get all deleted at once (provided you hadn't created any external references to them), but they are still multiple objects in the garbage collector. The memory allocation table must have an entry for the collector class, another for the array, and another entry for each and every Foo object. That's a lot of overhead for the cleanup code... -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
May 05 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CD62766.DC5AFE3E deming-os.org...

 OddesE wrote:
 Use a collection class:


 This way you can allocate a whole bunch of Foo's
 in one swoop. Also the Foo's will act as a single
 entity to the garbage collector because they are
 all contained in the same FooCollection.

They might get all deleted at once (provided you hadn't created any

 references to them), but they are still multiple objects in the garbage
 collector.  The memory allocation table must have an entry for the

 class, another for the array, and another entry for each and every Foo

 That's a lot of overhead for the cleanup code...

 --
 The Villagers are Online! http://villagersonline.com

 .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
 .[ (a version.of(English).(precise.more)) is(possible) ]
 ?[ you want.to(help(develop(it))) ]

I see what you mean. I don't know how important this is to you. I thought you were looking for a syntax sugar like solution to allocating and freeing a whole bunch of objects in one swoop. In that case a collection might help. If you also need the extra performance, then I think this would mean a serious change in the language / gc... Maybe there is a different solution? If your objects do not contain much logic, you might change them to structs? Walter are structs also garbage collected and 'reference-counted' as it were, or are they allocated on the stack? -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
May 06 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:ab6925$272k$1 digitaldaemon.com...

 Walter are structs also garbage collected and
 'reference-counted' as it were, or are they
 allocated on the stack?

It depends. You may declare them on stack, or you may allocate them using gc.malloc().
May 06 2002
prev sibling next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
OddesE wrote:

 I see what you mean.
 I don't know how important this is to you.
 I thought you were looking for a syntax sugar
 like solution to allocating and freeing a whole
 bunch of objects in one swoop.

I was looking for both, frankly :) Syntax sugar is nice...more efficient GC is nice, too. Neither are horribly important to me. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
May 06 2002
parent "Walter" <walter digitalmars.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CD6B5B4.AC9EDA1C deming-os.org...
 I was looking for both, frankly :)  Syntax sugar is nice...more efficient

 is nice, too.  Neither are horribly important to me.

D also supports using alloca(), which is something I use a lot.
May 15 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:ab6925$272k$1 digitaldaemon.com...
 Walter are structs also garbage collected and
 'reference-counted' as it were, or are they
 allocated on the stack?

structs are gc'd if they are allocated with new, and they are on the stack if they are declared there. You can allocate them with c.stdlib.malloc if you want <g>.
May 15 2002