GC interpreting integer values as pointers

%u e at ee.com
Mon Oct 11 11:53:03 PDT 2010


== Quote from Ivo Kasiuk (i.kasiuk at gmx.de)'s article
> > ~snip
> > > ----------------------------------------
> > > This writes:
> > > new uint
> > > no reference
> > > ========== reference, f7490e20, f7490e10, f7490df0,
>  f74
> > > 90dd0
> > > AA
> > > struct
> > > uint
> > > reference
> ...
> > > Thanks,
> > > Ivo
> >
> > In D1:
> ...
> > Writes:
> > no reference
> > ========== reference, ad3fd0, ad3fb0, ad3f90
> > new uint  << ;)
> > AA
> > uint
> > reference
> Thanks for trying it out in D1.
> So, summing up this means that:
> - In most cases, memory is by default scanned for pointers regardless of
> the actual data types.
> - In D2, newly allocated memory for a non-pointer data type (like "new
> uint" or "new uint[10]") is not scanned by default.
Isn't p a pointer data type?
I didn't even know I could do "i = new int;" :D

> - In D1, you have to use hasNoPointers if you want some memory not to be
> scanned.
> Is this observation correct?
> And what about structs/classes that have integer fields as well as
> pointer/reference fields?
> And what about associative arrays - apparently these are scanned even if
> the type is uint?
> Ivo

I added the struct again and also ran without the enclosing X class.

With X :
no reference
========== reference, ad3fd0, ad3fc0, ad3fa0, ad3f80
new uint
AA
struct
uint
reference

Without X :
no reference
========== reference, ad2fd0, ad2fc0, ad2fa0, ad2f80
new uint

--
import std.stdio;
import std.gc;

class C {
	string s;
	this(string s) { this.s=s; }
	~this() { writefln(s); }
}

struct S {
	uint r;
	static S opCall(uint x) {
		S s;
		s.r = x;
		return s;
	}
}

class X{
	C c;
	uint r;
	S s;
	uint[int] a;
	uint* p;

	this() {
		c = new C("reference");
		new C("no reference");
		r = cast(uint) cast(void*) new C("uint");
		s = S(cast(uint) cast(void*) new C("struct"));
		a[0] = cast(uint) cast(void*) new C("AA");
		p = new uint;
		*p = (cast(uint) cast(void*) new C("new uint"));
	}

}

void main(string[] args)
{
	/+
	c = new C("reference");
	new C("no reference");
	r = cast(uint) cast(void*) new C("uint");
	s = S(cast(uint) cast(void*) new C("struct"));
	a[0] = cast(uint) cast(void*) new C("AA");
	p = new uint;
	*p = (cast(uint) cast(void*) new C("new uint"));
	+/
	X x = new X;
	std.gc.fullCollect();
	writefln("========== %s, %x, %x, %x, %x", x.c.s, x.r, x.s.r, x.a[0],*x.p);
	//writefln("========== %s, %x, %x, %x, %x", c.s, r, s.r, a[0],*p);
}


More information about the Digitalmars-d-learn mailing list