A devil self reference

Alex sascha.orlov at gmail.com
Wed Aug 15 22:53:09 UTC 2018


On Wednesday, 15 August 2018 at 21:42:11 UTC, Jonathan M Davis 
wrote:
>
> What are you actually trying to do? Aside from the circular 
> reference issues, using the address of a struct like that is 
> very risky, because if the struct is ever moved, it will change.

Yeah... my functions are all ref. Or work with slices...

> Also, even if the circular reference worked, the struct is then 
> in a dynamic array on the heap, whereas the dynamic array 
> itself is on the stack. So, you're trying to subtract a stack 
> pointer from a heap pointer.

Hm... didn't thought of that. Does this matter?


My current form is like that:

´´´
void main()
{
	S.c.sarr.length = 5;
	S.c.sarr[2 .. 4].usefulFun;
}

struct Container
{
	S[] sarr;
}

struct S
{
	static Container c;

	size_t id()
	{
		return &this - c.sarr.ptr;
	}

	void anotherUsefulFun()
	{
	    /*
                 works on "neighbors" of this, which are only 
available,
                 if c.sarr is "well-known"
             */
	}
}

void usefulFun(S[] slice)
{
	import std.stdio;
	writeln(slice[0].id);
}
´´´

It works. After a lot of debugging and head aches, it works as I 
want to. But, only until I try to copy the array, as you said.
I'm aware of the problem, and I worked it out so far, that on 
copies of the array sarr only the operations are done, which do 
not need the pointer stuff.

Like dumping the data, for example, or querying elementary 
properties. Even querying for id on a array copy is ok, if I do 
it with another function, let's call it indexOf :)
What doesn't work are the calls to "neighbor"-acting functions, 
but these are not needed on sarr copies.

The project is almost finished, so, I'm mainly after a learn 
effect now. What I'm looking for, is something what would 
simplify my structure. So... I can think at least of two things:
1. something, where I don't need the dangerous pointer stuff.
2. something, which works well after I copy it.

If the circular stuff worked, it would simplify my life a lot. I 
could define some constructors, for copying data between arrays, 
whereas the pointer stuff would maintain itself, as it would be 
"integrated" into the type. But ok, I see, that this is at least 
kind of strange...


More information about the Digitalmars-d-learn mailing list