digitalmars.D - about destroy and delete.
- Dsby (34/34) Apr 20 2016 I see https://dlang.org/deprecate.html#delete
- Dsby (1/1) Apr 20 2016 And ,will destroy mark the memory in GC to be free?
- Daniel Kozak (11/14) Apr 20 2016 if you call destroy on struct pointer it is same as assign null
- Dsby (2/16) Apr 21 2016 Thanks for all.
- Lass Safin (4/38) Apr 20 2016 This is according to the reference, however this behavior should
- Marco Leise (12/12) Apr 20 2016 The semantics of `delete` from C++ are pretty clear. It is
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
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
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:Thanks for all.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 21 2016
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
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