alias this and struct allocation

Alex sascha.orlov at gmail.com
Mon May 6 15:22:49 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?

Yes:
static assert(!__traits(compiles, Child.init.base is null));

> Or is base definitely part of the allocation of Child on the 
> heap?

I would say, yes. Why do you think it is a contradiction?


More information about the Digitalmars-d-learn mailing list