title: trouble converting void * back to struct
body: <p>I'm working on a game using Box2D for physics and having trouble with void *. </p>
  
  <p>Box2D fixtures have a (void*) userData field, for doors in my level, I am setting that to the address of the door information, which is a struct called NextLevel (below).</p>
  
  <pre><code>struct NewLevel {
      bool changed;
      std::string name;
      int x;
      int y;
      int posx;
      int posy;
      int num;
  };
  </code></pre>
  
  <p>The doors are stored in the level class in <code>std::vector&lt;NewLevel&gt; doors</code>. In the loop that assembles each of the levels, if there is a door, I <code>doors.push_back(NewLevel())</code> and then set the data accordingly.  Then I store the <code>FixtureDef.userData = &amp;doors.back()</code>.</p>
  
  <p>However, when I go to retrieve that information, it doesn't work out. (fixB being a pointer to the fixture in contact).</p>
  
  <p><code>NewLevel nextDoor = *(static_cast&lt;NewLevel*&gt;(fixB-&gt;GetUserData()))</code></p>
  
  <p>Sometimes, it just works. However, at other times, it gives gibberish. When I load the next level, I clear the vector. I print the pointers out a lot, sometimes, they don't match, what would cause this? Is there a better way to pass this data?</p>
  
  <p>Thanks! (I can put more code if needed)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I ended up solving this by storing the b2Fixture* from box2d and the index of the item in the doors vector, rather than trying to cast/re-cast a pointer to the NewLevel and it has been working great. Thanks for all of the help, the notion of tracking the index got me thinking in the right direction. </p>