Jump to page: 1 2
Thread overview
Arrays of Class References
May 03, 2002
Russ Lewis
May 03, 2002
Walter
May 04, 2002
Russ Lewis
May 05, 2002
Walter
May 05, 2002
Pavel Minayev
May 05, 2002
OddesE
May 06, 2002
Russ Lewis
May 06, 2002
OddesE
May 06, 2002
Pavel Minayev
May 06, 2002
Russ Lewis
May 16, 2002
Walter
May 16, 2002
Walter
May 03, 2002
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 04, 2002
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 05, 2002
"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?
> It separates the algorithm from the intent.  The intent is, "create an
array
> 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
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.

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:
>
> > "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))) ]
>
>


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 06, 2002
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
> 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))) ]
> >
> >
>
> 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 06, 2002
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CD62766.DC5AFE3E@deming-os.org...
>
<SNIP>
>
> OddesE wrote:
> >
> > Use a collection class:
> >
<SNIP>
> >
> > 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))) ]
>


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))) ]


« First   ‹ Prev
1 2