www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Allocating and freeing memory like this?

reply "Bienlein" <jeti789 web.de> writes:
Hello,

ich found this article on the Internet that explains how to do 
malloc and free in D: 
http://fgda.pl/post/8/a-look-at-the-d-programming-language See 
the functions named _new and _delete. My question is whether this 
is really the way to allocate and free some memory for a class 
manually in D. Because I just did this playing around with D and 
it seems to work:

class D {
	public int i;
}

void main(string[] args)
{
	auto d = new D();
	delete(d);

         d.i = 123; // creates Access Violation as expected
}

The article confused me. Is the contents outdated or am I messing 
something up?

Thanks for shedding any light on this for me ;-).
Regards, Bienlein
Feb 26 2014
next sibling parent reply "Tolga Cakiroglu" <tcak pcak.com> writes:
I can't remember where I read it though, in documentation 
probably, the `free` function calls the `delete` function as 
well, and it can be used to remove object from memory.

Since there is garbage collector in the background already, you 
don't have to do it in this way (and I am not doing as well), but 
you are able.

While writing the message, found the documentation. 


On Wednesday, 26 February 2014 at 09:14:42 UTC, Bienlein wrote:
 Hello,

 ich found this article on the Internet that explains how to do 
 malloc and free in D: 
 http://fgda.pl/post/8/a-look-at-the-d-programming-language See 
 the functions named _new and _delete. My question is whether 
 this is really the way to allocate and free some memory for a 
 class manually in D. Because I just did this playing around 
 with D and it seems to work:

 class D {
 	public int i;
 }

 void main(string[] args)
 {
 	auto d = new D();
 	delete(d);

         d.i = 123; // creates Access Violation as expected
 }

 The article confused me. Is the contents outdated or am I 
 messing something up?

 Thanks for shedding any light on this for me ;-).
 Regards, Bienlein
Feb 26 2014
parent "Tolga Cakiroglu" <tcak pcak.com> writes:
I noticed that the `delete` function is not in that link. Anyway, 
there are some discussions about the `delete` function already.

http://forum.dlang.org/thread/bmipphidczppnllpincm forum.dlang.org#post-mailman.1327.1351300092.5162.digitalmars-d-learn:40puremagic.com

On Wednesday, 26 February 2014 at 09:19:55 UTC, Tolga Cakiroglu 
wrote:
 I can't remember where I read it though, in documentation 
 probably, the `free` function calls the `delete` function as 
 well, and it can be used to remove object from memory.

 Since there is garbage collector in the background already, you 
 don't have to do it in this way (and I am not doing as well), 
 but you are able.

 While writing the message, found the documentation. 

Feb 26 2014
prev sibling next sibling parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Wednesday, 26 February 2014 at 09:14:42 UTC, Bienlein wrote:

A similar approach is already employed in phobos, see 


 The article confused me. Is the contents outdated or am I 
 messing something up?
Regarding keywords new and delete: http://dlang.org/class.html#allocators. Read the Notes carefully :) In a nutshell, 'delete' keyword will eventually go away entirely, and it won't be allowed to redefine 'new'. Regarding the code in the article: import std.stdio, std.conv, core.stdc.stdlib; T _new(T, Args...) (Args args) { size_t objSize = __traits(classInstanceSize, T); void* tmp = core.stdc.stdlib.malloc(objSize); if (!tmp) throw new Exception("Memory allocation failed"); Calling 'new Exception' when memory allocation failed is a bad idea. There is std.exception.onOutOfMemoryError() function for such cases. void _delete(T)(T obj) { clear(obj); destroy(obj) should be used instead. It's possible that some new idioms will come up once std.allocator arrives into Phobos.
Feb 26 2014
prev sibling parent "Mike" <none none.com> writes:
On Wednesday, 26 February 2014 at 09:14:42 UTC, Bienlein wrote:
 Hello,

 ich found this article on the Internet that explains how to do 
 malloc and free in D: 
 http://fgda.pl/post/8/a-look-at-the-d-programming-language See 
 the functions named _new and _delete. My question is whether 
 this is really the way to allocate and free some memory for a 
 class manually in D. Because I just did this playing around 
 with D and it seems to work:

 class D {
 	public int i;
 }

 void main(string[] args)
 {
 	auto d = new D();
 	delete(d);

         d.i = 123; // creates Access Violation as expected
 }

 The article confused me. Is the contents outdated or am I 
 messing something up?

 Thanks for shedding any light on this for me ;-).
 Regards, Bienlein
You need to clarify if you want to allocate on the managed heap (managed by the garbage collector) or the unmanaged heap (managed by you with malloc and free) The _new and _delete methods in the article use an outdated syntax as class allocators[1] (read "new") and deallocators[2] (read "delete") are scheduled for deprecation. There is also an article[3] here on dlang.org that also shows and example of this, but it also uses the old syntax. Coincidently, I just posted some code[4] on the digitalmars.D list asking for some help updating that very example. I intend to submit a pull request to update it in the next day or two. Mike [1] http://dlang.org/class.html#deallocators [2] http://dlang.org/class.html#allocators [3] http://dlang.org/memory.html#newdelete [4] http://forum.dlang.org/post/zxswyzstaepsatiyjjdt forum.dlang.org
Feb 26 2014