www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scope statement

reply Lars V <nospamplease nospam.com> writes:
Hi, 

I have a question: 

void foo() 
{ 
  Obj o = new Obj(); 
}

In this code, the destructor of the object will be called ALWAYS at the end of
the function? 

In D, the objects are not destroyed when they go out of their scopes??? Is
necessary to use the "scope" statement? 

I really don't understand the necessity of the scope statement... could anybody
explain it?

Thanks, 
Regards
Feb 05 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Lars V wrote:
 Hi, 
 
 I have a question: 
 
 void foo() 
 { 
   Obj o = new Obj(); 
 }
 
 In this code, the destructor of the object will be called ALWAYS at the end of
the function? 
 
 In D, the objects are not destroyed when they go out of their scopes??? Is
necessary to use the "scope" statement? 
 
 I really don't understand the necessity of the scope statement... could
anybody explain it?
Yes you need to use scope if you want deterministic destruction. Allocation and deallocation are expensive, and one of the nice things about a garbage collection system is that it lets you do clean-up more lazily. That way you can handle a bunch of de-allocations at once. Think of it as waiting a week to vacuum up the room instead of pulling out the vacuum cleaner every time a piece of dust hits the floor. --bb
Feb 05 2008
parent reply Lars V <nospamplease nospam.com> writes:
Bill Baxter Wrote:

 Lars V wrote:
 Hi, 
 
 I have a question: 
 
 void foo() 
 { 
   Obj o = new Obj(); 
 }
 
 In this code, the destructor of the object will be called ALWAYS at the end of
the function? 
 
 In D, the objects are not destroyed when they go out of their scopes??? Is
necessary to use the "scope" statement? 
 
 I really don't understand the necessity of the scope statement... could
anybody explain it?
Yes you need to use scope if you want deterministic destruction. Allocation and deallocation are expensive, and one of the nice things about a garbage collection system is that it lets you do clean-up more lazily. That way you can handle a bunch of de-allocations at once. Think of it as waiting a week to vacuum up the room instead of pulling out the vacuum cleaner every time a piece of dust hits the floor. --bb
But, when the function ends the object should be collected by the GC (there are no more references to it) and the GC will call the destructor, no? I don't understand it.
Feb 05 2008
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
Lars V <nospamplease nospam.com> wrote:

 Bill Baxter Wrote:

 Lars V wrote:
 Hi,

 I have a question:

 void foo()
 {
   Obj o =3D new Obj();
 }

 In this code, the destructor of the object will be called ALWAYS at=
=
 the end of the function?
 In D, the objects are not destroyed when they go out of their  =
 scopes??? Is necessary to use the "scope" statement?
 I really don't understand the necessity of the scope statement...  =
 could anybody explain it?

 Yes you need to use scope if you want deterministic destruction.

 Allocation and deallocation are expensive, and one of the nice things=
 about a garbage collection system is that it lets you do clean-up mor=
e
 lazily.  That way you can handle a bunch of de-allocations at once.
 Think of it as waiting a week to vacuum up the room instead of pullin=
g
 out the vacuum cleaner every time a piece of dust hits the floor.

 --bb
But, when the function ends the object should be collected by the GC =
 (there are no more references to it) and the GC will call the  =
 destructor, no? I don't understand it.
When the object goes out of scope, there are no more pointers to it, = correct. This only means however, that the next time the GC runs a = collection cycle (which might be right after you've exited the scope, bu= t = you can't know that), the object's destructor will be called. If you instead use void foo() { scope Obj o =3D new Obj(); } You are guaranteed that the moment the object goes out of scope, its = destructor will be called. -Simen
Feb 05 2008
prev sibling parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2008-02-05 18:26:50 -0500, Lars V <nospamplease nospam.com> said:

 But, when the function ends the object should be collected by the GC 
 (there are no more references to it) and the GC will call the 
 destructor, no? I don't understand it.
The garbage collector will generally destruct and deallocate the object when you ask for more memory, if there are no more reference to it (it doesn't scan memory for pointers each time a function returns). This could happen the next time you call new, or in a few more calls to new. Basically, when to dispose of the object is left to the discretion of the garbage collector which may optimize things in various ways you shouldn't have to care much about. If you need deterministic destruction for your object, either use scope or call the destructor yourself (using delete). -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2008
parent reply Lars V <nospamplease nospam.com> writes:
Michel Fortin Wrote:

 On 2008-02-05 18:26:50 -0500, Lars V <nospamplease nospam.com> said:
 
 But, when the function ends the object should be collected by the GC 
 (there are no more references to it) and the GC will call the 
 destructor, no? I don't understand it.
The garbage collector will generally destruct and deallocate the object when you ask for more memory, if there are no more reference to it (it doesn't scan memory for pointers each time a function returns). This could happen the next time you call new, or in a few more calls to new. Basically, when to dispose of the object is left to the discretion of the garbage collector which may optimize things in various ways you shouldn't have to care much about. If you need deterministic destruction for your object, either use scope or call the destructor yourself (using delete). -- Michel Fortin michel.fortin michelf.com http://michelf.com/
OK, thanks for all the replies. Only one more question; the deterministic destruction of the object -using scope or delete- will force the GC to collect it? or it'll only call the destructor?
Feb 05 2008
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2008-02-05 20:00:38 -0500, Lars V <nospamplease nospam.com> said:

 Only one more question;
 the deterministic destruction of the object -using scope or delete- 
 will force the GC to collect it? or it'll only call the destructor?
It'll call the destructor and free the memory, but I wouldn't say it's the garbage collector that does it in this case. Note that when you use scope, the compiler may optimise things and allocate the object directly on the stack. In this case, the destructor is (implicitly) called but there is no memory that needs to be freed after the function call. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2008