On Thursday, 21 August 2014 at 14:09:24 UTC, Ricky C wrote:
I'm trying to make a tree data structure in part of my first non-trivial D-based program. Turns out that DMD likes to re-use PODs - sounds good, but that trapped me, and I'm not sure how to clear myself out of the problem nicely.

My simplified, but not oversimplified, example is http://dpaste.dzfl.pl/67b022ca029e

You'll note in the output of the program several duplicated values for "self" and the parent value goes to places other than the parent. This is all wrong, and shows my ignorance, but is as far as I've been able to work it.

In my operational code I bypassed the problem by storing every leaf and branch in an array and changing the self, parent, and children properties to be indices into said array. But this feels hackish: I'd rather have the tree itself hold everything it needs.

Note: I'm not stuck with a struct. I could class the thing, but I figured that a struct holds what I need simply...

Thoughts, solutions?

As far as I can see, you're overwriting old trees with new ones.
You store everything in tile_trees, and use pointers to its data.
When you then "Copy the trees into the branches and clear the
forest for the next pass", the pointers still point to the now
cleared old locations in tile_trees. You fill tile_trees again,
reusing the same locations and pointers.

Maybe try
     TileTree*[string] tile_trees;
and
     auto new_tree = new TileTree(...);
or make TileTree a class.

Reply via email to