D - Arrays of Class References
- Russ Lewis (19/19) May 03 2002 So let's says that you want to allocate a whole bunch of class objects
- Walter (3/17) May 03 2002 Why is it a nightmare?
- Russ Lewis (13/31) May 04 2002 It separates the algorithm from the intent. The intent is, "create an a...
- Walter (10/19) May 05 2002 array
- Pavel Minayev (3/5) May 05 2002 Oh yes, I remember your reply to suggestions of for-each loop... =)
- OddesE (35/67) May 05 2002 and
- Russ Lewis (11/79) May 05 2002 They might get all deleted at once (provided you hadn't created any exte...
-
OddesE
(33/52)
May 06 2002
"Russ Lewis"
wrote in message - Pavel Minayev (4/7) May 06 2002 It depends. You may declare them on stack, or you may allocate them
- Russ Lewis (8/13) May 06 2002 I was looking for both, frankly :) Syntax sugar is nice...more efficien...
- Walter (4/6) May 15 2002 GC
- Walter (5/8) May 15 2002 structs are gc'd if they are allocated with new, and they are on the sta...
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
"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
Walter wrote:"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CD300C7.2814DFC4 deming-os.org...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))) ]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 04 2002
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CD387A9.5778D3E5 deming-os.org...Walter wrote:arrayWhy is it a nightmare?It separates the algorithm from the intent. The intent is, "create anof 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 isaseparate object in the garbage collector. Having per-object collection is usually nice, but in some cases it doesn't make sense. In some programs,itwould be nice to be able to allocate a large block of objects and havethemcount 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
"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
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CD387A9.5778D3E5 deming-os.org...Walter wrote:and"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 oncearrayIt separates the algorithm from the intent. The intent is, "create anbuild an array of references to them?Why is it a nightmare?of Foo's". The algorithm is something very different. Also, it exposed the problem that (as I understand it) each Foo object isaseparate object in the garbage collector. Having per-object collection is usually nice, but in some cases it doesn't make sense. In some programs,itwould be nice to be able to allocate a large block of objects and havethemcount 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
OddesE wrote:"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CD387A9.5778D3E5 deming-os.org...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))) ]Walter wrote:and"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 oncearrayIt separates the algorithm from the intent. The intent is, "create anbuild an array of references to them?Why is it a nightmare?of Foo's". The algorithm is something very different. Also, it exposed the problem that (as I understand it) each Foo object isaseparate object in the garbage collector. Having per-object collection is usually nice, but in some cases it doesn't make sense. In some programs,itwould be nice to be able to allocate a large block of objects and havethemcount 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.
May 05 2002
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message news:3CD62766.DC5AFE3E deming-os.org...<SNIP>OddesE wrote:<SNIP>Use a collection class:externalThis 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 anyreferences to them), but they are still multiple objects in the garbage collector. The memory allocation table must have an entry for thecollectorclass, another for the array, and another entry for each and every Fooobject.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
"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
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
"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 efficientGCis nice, too. Neither are horribly important to me.D also supports using alloca(), which is something I use a lot.
May 15 2002
"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