March 14, 2005
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


March 15, 2005
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
>
>


March 15, 2005
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 :)


March 15, 2005
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
March 15, 2005
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
March 15, 2005
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.
Top | Discussion index | About this forum | D home