View mode: basic / threaded / horizontal-split · Log in · Help
October 30, 2005
How does D allocate objects?
Coming from previous discussions about the GC and reading later answers, i think
i'm a little confused: 
1) Objects in D are ONLY heap-allocated and never stack-allocated?
2) Can you create static objects in D?

If the anwer to (2) is NO then i suppose something like this is forbidden:

MyObject myobj_instance(initializer params...);

Thanks


Tom
October 30, 2005
Re: How does D allocate objects?
Tomás Rossi wrote:
> Coming from previous discussions about the GC and reading later answers, i think
> i'm a little confused: 
> 1) Objects in D are ONLY heap-allocated and never stack-allocated?
> 2) Can you create static objects in D?
> 
> If the anwer to (2) is NO then i suppose something like this is forbidden:
> 
> MyObject myobj_instance(initializer params...);
> 
> Thanks
> 
> 
> Tom

As far as I understand, Objects in D are always references, never values.

There are certain circumistances where objects maybe allocated on the 
stack, but even then, the "new" keyword must be used! And I suspect that 
these circumistances are not guaranteed to create the objects on the 
stack, i.e. it's the compiler's dicision (implementation dependet).

I only know one such circumistance: auto (raii) objects that don't have 
a destructor.
October 30, 2005
Re: How does D allocate objects?
"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message 
news:dk1buh$1jcb$1@digitaldaemon.com...
> 1) Objects in D are ONLY heap-allocated and never stack-allocated?

You can override this behavior by writing your own class allocator (custom 
new operator per-class).  It's in the docs: 
http://www.digitalmars.com/d/memory.html#stackclass

> 2) Can you create static objects in D?

You mean something like

class A
{
   static A someA;

   static this()
   {
       someA = new A();
   }
}

That?  or have I missed your point?
October 31, 2005
Re: How does D allocate objects?
Tomás Rossi wrote:

> 1) Objects in D are ONLY heap-allocated and never stack-allocated?

Struct instances can be alloctated statically:

SomeStruct s;
s.somefield = 1;

Class instances are heap allocated and can only be newed:

MyClass c = new MyClass(); // <- OK
MyClass c();	// <-- not OK

> 2) Can you create static objects in D?
> 
> If the anwer to (2) is NO then i suppose something like this is forbidden:
> 
> MyObject myobj_instance(initializer params...);

If you need to do this in a method/function for some class instance that 
you wish to be auto-destructed when it goes out of scope, use the auto 
keyword:

void MyFunc()
{
   // c will be destructed when it goes out of scope
   auto MyClass c = new MyClass();
}

If there's something you wish to statically allocate during app startup, 
use module constructors.

module mypackage.mymodule;

class MyClass
{
   public this()
   {
      doSomething();
   }
}
// 'static' instance
MyClass c;

// module constructor
static this()
{
   c = new MyClass();
}

// you can also have module destructors
static ~this()
{
}
October 31, 2005
Re: How does D allocate objects?
In article <dk3dso$pf1$1@digitaldaemon.com>, Jarrett Billingsley says...
>
>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message 
>news:dk1buh$1jcb$1@digitaldaemon.com...
>> 1) Objects in D are ONLY heap-allocated and never stack-allocated?
>
>You can override this behavior by writing your own class allocator (custom 
>new operator per-class).  It's in the docs: 
>http://www.digitalmars.com/d/memory.html#stackclass
>
>> 2) Can you create static objects in D?
>
>You mean something like
>
>class A
>{
>    static A someA;
>
>    static this()
>    {
>        someA = new A();
>    }
>}
>
>That?  or have I missed your point? 

I guess that would be a good example.

PS: The example i gave was about question 1).

Tom
October 31, 2005
Re: How does D allocate objects?
In article <dk1buh$1jcb$1@digitaldaemon.com>, Tomás Rossi says...
>
>Coming from previous discussions about the GC and reading later answers, i think
>i'm a little confused: 
>1) Objects in D are ONLY heap-allocated and never stack-allocated?
>2) Can you create static objects in D?
>
>If the anwer to (2) is NO then i suppose something like this is forbidden:
>
>MyObject myobj_instance(initializer params...);
>
>Thanks
>
>
>Tom

Here's a number of ways to allocate class objects:

http://digitalmars.com/d/memory.html
October 31, 2005
Re: How does D allocate objects?
In article <dk3oob$1c5c$1@digitaldaemon.com>, Mike Parker says...
>
>Tomás Rossi wrote:
>
>> 1) Objects in D are ONLY heap-allocated and never stack-allocated?
>
>Struct instances can be alloctated statically:
>
>SomeStruct s;
>s.somefield = 1;
>
>Class instances are heap allocated and can only be newed:
>
>MyClass c = new MyClass(); // <- OK
>MyClass c();	// <-- not OK
>
>> 2) Can you create static objects in D?
>> 
>> If the anwer to (2) is NO then i suppose something like this is forbidden:
>> 
>> MyObject myobj_instance(initializer params...);
>
>If you need to do this in a method/function for some class instance that 
>you wish to be auto-destructed when it goes out of scope, use the auto 
>keyword:
>
>void MyFunc()
>{
>    // c will be destructed when it goes out of scope
>    auto MyClass c = new MyClass();
>}
>
>If there's something you wish to statically allocate during app startup, 
>use module constructors.
>
>module mypackage.mymodule;
>
>class MyClass
>{
>    public this()
>    {
>       doSomething();
>    }
>}
>// 'static' instance
>MyClass c;
>
>// module constructor
>static this()
>{
>    c = new MyClass();
>}
>
>// you can also have module destructors
>static ~this()
>{
>}

Ok thank you very much!, you've cleared my mind.

Tom
Top | Discussion index | About this forum | D home