www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I do new/delete instead of GC?

reply "Craig Black" <cblack ara.com> writes:
How do you use new/delete instead of GC?  Are you stuck using struct instead 
of class?  If so, then I guess I can't define constructors when I use 
new/delete.

-Craig 
Mar 14 2005
next sibling parent "Kris" <fu bar.com> writes:
Read these:

http://digitalmars.com/d/class.html

http://digitalmars.com/d/memory.html



"Craig Black" <cblack ara.com> wrote in message
news:d158e8$2s78$1 digitaldaemon.com...
 How do you use new/delete instead of GC?  Are you stuck using struct
instead
 of class?  If so, then I guess I can't define constructors when I use
 new/delete.

 -Craig
Mar 14 2005
prev sibling next sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Interesting question in fact.

This way it should work:

MyClass c = new MyClass();
delete c;

class instances are the heap beasts.

structs in D are 'primitive' types ( int, double, etc. )
E.g. to *allocate* an int you need to do int* myintptr = new int;
And to delete - delete myintptr;

Same for structures:

struct MyStruct { ... }

MyStruct *pms = new MyStruct;
delete pms;

I am not sure though :)
Mar 14 2005
parent Derek Parnell <derek psych.ward> writes:
On Mon, 14 Mar 2005 16:47:06 -0800, Andrew Fedoniouk wrote:

 Interesting question in fact.
 
 This way it should work:
 
 MyClass c = new MyClass();
 delete c;
 
 class instances are the heap beasts.
 
 structs in D are 'primitive' types ( int, double, etc. )
 E.g. to *allocate* an int you need to do int* myintptr = new int;
 And to delete - delete myintptr;
 
 Same for structures:
 
 struct MyStruct { ... }
 
 MyStruct *pms = new MyStruct;
 delete pms;
 
 I am not sure though :)
Yes that's right. <code> import std.stdio; struct S { real r; } void main() { int* x; S* y; x = new int; y = new S; *x = 4; y.r = 5.6; writefln("x = %d, y = %f", *x, y.r); delete x; delete y; } </code> -- Derek Melbourne, Australia 15/03/2005 11:58:48 AM
Mar 14 2005
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Craig Black wrote:
 How do you use new/delete instead of GC?  Are you stuck using struct instead 
 of class?  If so, then I guess I can't define constructors when I use 
 new/delete.
Everything (even structs) is under control of the GC. structs will get cleaned up like everything else, when there are no references to them. Likewise with anything else allocated with new, such as arrays or individual ints. However, you can explicitly delete anything, if you want; but you are responsible for making sure that the thing you are deleting hasn't already been collected by the GC, and that you never use it again. If you want the GC to not collect your data, then you have two choices: 1) use malloc/free 2) Keep a reference to the thing
Mar 14 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Russ Lewis wrote:
<snip>
 However, you can explicitly delete anything, if you want; but you are 
 responsible for making sure that the thing you are deleting hasn't 
 already been collected by the GC, and that you never use it again.
<snip> If you still have a reference to the thing in order to delete it, of course it won't be collected by the GC. Of course you could (for the time being) use the tricks it tells you not to to hide the pointer from the GC, but what would be the point? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Mar 15 2005