alias this and struct allocation

aliak something at something.com
Mon May 6 15:17:37 UTC 2019


On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote:
> I've been having some memory issues (referenced objects turning 
> to nulls for no apparent reason) and I was wondering if I've 
> misunderstood how allocation works when instantiating a struct 
> that uses alias this:
>
> 	import std.stdio;
> 	
> 	struct Parent {
> 		int a;
> 	}
> 	struct Child {
> 		Parent base;
> 		alias base this;
> 		int y;
> 	}
> 	auto myStructMaker() {
> 		return new Child(Parent(10),20);
> 	}
> 	
> 	void main()
> 	{
> 		writeln(*myStructMaker());
> 	}
>
> In this example is the data in base guaranteed to exist? Or is 
> base definitely part of the allocation of Child on the heap?

Base exists as a value type inside Child so if Child exists, then 
base is definitely there. If base was a class or a pointer to a 
struct, then it may or may not exist.

Here's an excellent post from HS Teoh that explains a lot of 
this: 
https://forum.dlang.org/post/mailman.2535.1417413189.9932.digitalmars-d-learn@puremagic.com

Do you have an example of a referenced object turning to null? We 
may be able to spot something


More information about the Digitalmars-d-learn mailing list