Allocating and freeing memory like this?

Mike none at none.com
Wed Feb 26 01:58:38 PST 2014


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


More information about the Digitalmars-d-learn mailing list