www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - about destroy and delete.

reply Dsby <dushibaiyu yahoo.com> writes:
I see https://dlang.org/deprecate.html#delete
The delete will be removeed,  when will be deprecate?

and i test destroy/GC.free and delte in struct, the value is 
difference;

struct Struct
{
     string value = "struct";
     ~this()
     {
         writeln(value);
     }
}

void main()
{

     auto s = new Struct();
     delete s;

     writeln("----------------");

}

will printf :
struct
----------------

But in
void main()
{

     auto s = new Struct();
     s.destroy;
     GC.free(s);

     writeln("----------------");

}

will printf :
----------------
struct

If I only GC.free(s); only printf: ----------------

so, I want to know why don't destroy direct printf ?
Apr 20 2016
next sibling parent Dsby <dushibaiyu yahoo.com> writes:
And ,will destroy mark the memory in GC to be free?
Apr 20 2016
prev sibling next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
 I see https://dlang.org/deprecate.html#delete
 ...
 so, I want to know why don't destroy direct printf ?
if you call destroy on struct pointer it is same as assign null to it so destroy(s) is same as s = null; OK it is more like s = (Struct*).init; But if you do (*s).destroy(), it will work (ok it will call destructor two times but thats not error) Or if you use class instead of struct it will works as you expected
Apr 20 2016
parent Dsby <dushibaiyu yahoo.com> writes:
On Wednesday, 20 April 2016 at 09:00:41 UTC, Daniel Kozak wrote:
 On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
 I see https://dlang.org/deprecate.html#delete
 ...
 so, I want to know why don't destroy direct printf ?
if you call destroy on struct pointer it is same as assign null to it so destroy(s) is same as s = null; OK it is more like s = (Struct*).init; But if you do (*s).destroy(), it will work (ok it will call destructor two times but thats not error) Or if you use class instead of struct it will works as you expected
Thanks for all.
Apr 21 2016
prev sibling next sibling parent Lass Safin <lasssafin gmail.com> writes:
On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
 I see https://dlang.org/deprecate.html#delete
 The delete will be removeed,  when will be deprecate?

 and i test destroy/GC.free and delte in struct, the value is 
 difference;

 struct Struct
 {
     string value = "struct";
     ~this()
     {
         writeln(value);
     }
 }

 void main()
 {

     auto s = new Struct();
     delete s;

     writeln("----------------");

 }

 will printf :
 struct
 ----------------

 But in
 void main()
 {

     auto s = new Struct();
     s.destroy;
     GC.free(s);

     writeln("----------------");

 }

 will printf :
 ----------------
 struct

 If I only GC.free(s); only printf: ----------------

 so, I want to know why don't destroy direct printf ?
This is according to the reference, however this behavior should probably be changed to match that of the class, which will call the destructor immediately.
Apr 20 2016
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
The semantics of `delete` from C++ are pretty clear. It is
meant for dynamically allocated memory. destroy(=E2=80=A6) however is
a generic tool that brings the thing you pass in back to an
initial state. For pointers, null is assigned, for structs and
classes (which are not pointers but references) the dtor is
called.
Making it do the same thing for an argument of struct type T
and T* should not be done lightly. It will break generic
code, where the location that calls destroy(=E2=80=A6) does not own
the pointed-to struct.

--=20
Marco
Apr 20 2016