Am 26.04.2011 21:26, schrieb Daniel Gibson:
Am 26.04.2011 20:23, schrieb Steven Schveighoffer:
On Tue, 26 Apr 2011 14:14:11 -0400, Benjamin Thaut
<[email protected]> wrote:
Am 26.04.2011 19:59, schrieb Steven Schveighoffer:
On Tue, 26 Apr 2011 13:36:37 -0400, Benjamin Thaut
<[email protected]> wrote:
I've been reading through various delete topics in the past hour, but
couldn't find any statement on how manual memory management would
look, if the delete operator is deprecated. Something like the
following seems really odd:
class foo {
public new(size_t sz){ //language support
return malloc(sz);
}
public void Delete(){ // no language support ??
this.__dtor();
free(this);
}
}
auto fooInst = new foo(); //language support
fooInst.Delete(); //no language support ??
IIUC, the custom allocator will be deprecated as well.
What I think you need to use instead is emplace (a phobos function, not
sure where it is), clear, and GC.free.
Andrei hinted at a not-yet-written drop-in replacement for delete, but
I'm not sure how that looks.
-Steve
I don't want to use the GC at all, and clear() seems inefficent, since
it reaintializes the object. This is unneccsary since I just want to
throw it away anyway.
This is no longer the case for classes. It actually runs the same
function that delete runs (rt_finalize). Though, I'm not sure it's in
2.052...
emplace only works for structs, it does not work for classes.
I have not used it, but I'm under the impression from Andrei that it is
to replace scope classes, so I would guess it has to work for them.
It *does* work for classes (as of dmd 2.052).
Could someone please write a small example how manual memory
management would look without new / delete?
I think that is a good idea.
Not a tutorial, just a simple example using C malloc/free:
http://pastebin.com/HSBrk5kA
Cheers,
- Daniel
Thanks for the example, thats exactly what I needed.
I still don't understand why the delete operator is deprecated
completely. It could be defined, that it is only useable if the new and
delete operator have been overloaded in the class or struct that is
tried to be deleted.
--
Kind Regards
Benjamin Thaut