title: Can pointers to items in other classes/structures be safely used?
body: <p>I have a fairly complex scenario, so I am trying to distill this down to the basic issue.</p>
  
  <p>I have 3 classes (simplified here):</p>
  
  <pre><code>class Base {
  protected:
      std::map &lt;unsigned int, Node*&gt; nodes;
  }
  
  class Node {
  protected:
      std::map &lt;unsigned int, Leaf*&gt; leaves;
  }
  
  class Leaf {
  protected:
      myDataStruct myData;
  }
  </code></pre>
  
  <p>These classes represent a 3-level tree consisting of a base, some number of intermediary nodes in a <code>std::map&lt;int,ptr*&gt;</code>, and some number of leaves per node, also as a <code>std::map&lt;int,ptr*&gt;</code>.  This tree is generated and updated dynamically in an update thread.</p>
  
  <p>I have another thread that renders data from these nodes, at a much slower rate than the updates, so I prefer to keep the update action separate from the rendering.  I do not render all of the data on every loop of the render thread either.</p>
  
  <p>My original idea was to register that I wanted to render data from a leaf by calling a function that stores a pointer to the data in the leaf:</p>
  
  <pre><code>void MyRenderClass::registerData(int* dataPointer) {
      // store dataPointer in MyRenderClass
      myStoredDataPointer = dataPointer;
  }
  </code></pre>
  
  <p>Then during the render thread loop, I use that data pointer to render the data.</p>
  
  <p>So, the question is: <strong>Is this safe?</strong>  I can handle the case where the leaf or node is deleted by un-registering the data in the respective destructors.  What I really want to know is, <strong>will that pointer stay the same as the tree is updated and as new nodes and leaves are added to it?</strong>  Will the <code>std::map</code> rearrange or move pointers as it is modified?</p>
  
  <p>On a related note, can I safely store pointers to the parent nodes/base in the leaves/nodes so I can traverse the tree from leaf->base?</p>
  
  <p>I hope this makes sense.  I thought this seemed ok at first, but I want to verify before I go further down this path.  The example is slightly contrived, as the entire code base is much too large to grok.</p>