Thread overview
alias this and struct allocation
May 06, 2019
faissaloo
May 06, 2019
aliak
May 06, 2019
faissaloo
May 06, 2019
Alex
May 06, 2019
Adam D. Ruppe
May 06, 2019
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?
May 06, 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
May 06, 2019
On Monday, 6 May 2019 at 15:17:37 UTC, aliak wrote:
> Do you have an example of a referenced object turning to null? We may be able to spot something

Unfortunately I haven't managed to produce an example any smaller than my entire codebase
May 06, 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?
May 06, 2019
On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote:
> misunderstood how allocation works when instantiating a struct that uses alias this:

alias this has no effect on allocation at all. All it does is if

x.y

doesn't compile, it rewrites it to

x.alias_this.y

(or if f(x) doesn't work, it tries f(x.alias_this) too, same idea though.)

That's all it does; it is a way to simplify access to or through a member.

So the allocation doesn't factor into it.

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

It is definitely part of the Child allocation. But if you are passing it to a function, keep in mind it still works as a struct.

void foo(Base b) {}

auto child = new Child();

foo(child);


That gets rewritten into

foo(child.base);

which is passed by value there, unlike classes where it is all references. So that might be a source of confusion for you.