GC interpreting integer values as pointers

Ivo Kasiuk i.kasiuk at gmx.de
Mon Oct 11 12:38:51 PDT 2010


> > > ~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

What I mean is that p is pointing to data which has a simple data type
(not a struct/class/union) that is not a pointer/reference type.
For instance, with "p = new uint[10]" the compiler knows that the newly
allocated memory that p points to does not contain any pointers. With
D2, that seems to cause the memory not to be scanned.


> > - 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
...

No suprises with the struct.
And the "Without X" example... I am not sure, with the variables all in
the current stack frame that might be a special case. What about global
variables instead:

...
C c;
uint r;
S s;
uint[int] a;
uint* p;
uint[] arr;
void f() {
  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"));
  arr = new uint[1];
  arr[0] = (cast(uint) cast(void*) new C("array"));
}
void main(string[] args) {
  f();
  GC.collect();
  writefln("========== %s, %x, %x, %x, %x, %x",
           c.s, r, s.r, a[0], *p, arr[0]);
}

That gives me (with D2):

array
new uint
no reference
========== reference, f74c3e20, f74c3e10, f74c3df0, f74c3dd0, f74c3db0
AA
struct
uint
reference




More information about the Digitalmars-d-learn mailing list