title: TOUGH: Dealing with deeply nested pointers in C++
body: <p>I define this structure:</p>
  
  <pre><code>struct s_molecule
  {
    std::string res_name;
    std::vector&lt;t_particle&gt; my_particles;
    std::vector&lt;t_bond&gt; my_bonds;
    std::vector&lt;t_angle&gt; my_angles;
    std::vector&lt;t_dihedral&gt; my_dihedrals;
  
    s_molecule&amp; operator=(const s_molecule &amp;to_assign)
    {
      res_name = to_assign.res_name;
      my_particles = to_assign.my_particles;
      my_bonds = to_assign.my_bonds;
      my_angles = to_assign.my_angles;
      my_dihedrals = to_assign.my_dihedrals;
      return *this;
    }
  };
  </code></pre>
  
  <p>and these structures:</p>
  
  <pre><code>typedef struct s_particle
  {
    t_coordinates position;
    double charge;
    double mass;
    std::string name;
    std::vector&lt;t_lj_param&gt;::iterator my_particle_kind_iter;
  
    s_particle&amp; operator=(const s_particle &amp;to_assign)
    {
      position = to_assign.position;
      charge = to_assign.charge;
      mass = to_assign.mass;
      name = to_assign.name;
      my_particle_kind_iter = to_assign.my_particle_kind_iter;
      return *this;
    }
  } t_particle;
  
  struct s_bond
  {
    t_particle * particle_1;
    t_particle * particle_2;
    std::vector&lt;t_bond_param&gt;::iterator my_bond_kind_iter;
  
    s_bond&amp; operator=(const s_bond &amp;to_assign)
    {
      particle_1 = to_assign.particle_1;
      particle_2 = to_assign.particle_2;
      my_bond_kind_iter = to_assign.my_bond_kind_iter;
      return *this;
    }
  };
  </code></pre>
  
  <p>and then in my code I return a pointer to an s_molecule (typedef'd to t_molecule, but still).</p>
  
  <p>Using this pointer I can get this code to work:</p>
  
  <pre><code>for  (unsigned int i = 0;
        i &lt; current_molecule-&gt;my_particles.size();
        i++)
      {
        std::cout &lt;&lt; "Particle " 
          &lt;&lt; current_molecule-&gt;my_particles[i].name &lt;&lt; std::endl
              &lt;&lt; "Charge: " 
          &lt;&lt; current_molecule-&gt;my_particles[i].charge &lt;&lt; std::endl
          &lt;&lt; "Mass: " 
          &lt;&lt; current_molecule-&gt;my_particles[i].mass &lt;&lt; std::endl
          &lt;&lt; "Particle Kind Name: " 
          &lt;&lt; (*current_molecule-&gt;my_particles[i].my_particle_kind_iter).atom_kind_name 
          &lt;&lt; std::endl
          &lt;&lt; "x: " &lt;&lt; current_molecule-&gt;my_particles[i].position.x 
          &lt;&lt; " y: " &lt;&lt; current_molecule-&gt;my_particles[i].position.y
      #ifdef USE_3D_GEOM
          &lt;&lt; "z: " &lt;&lt; current_molecule-&gt;my_particles[i].position.z
      #endif
          &lt;&lt; std::endl;
      }
  </code></pre>
  
  <p>If I replace it with:</p>
  
  <pre><code>for  (std::vector&lt;t_particle&gt;::iterator it = current_molecule-&gt;my_particles.begin();
        it !=current_molecule-&gt;my_particles.end();
        it++)
      {
        std::cout &lt;&lt; "Particle " 
          &lt;&lt; (*it).name &lt;&lt; std::endl
              &lt;&lt; "Charge: " 
          &lt;&lt; (*it).charge &lt;&lt; std::endl
          &lt;&lt; "Mass: " 
          &lt;&lt; (*it).mass &lt;&lt; std::endl
          &lt;&lt; "Particle Kind Name: " 
          &lt;&lt; (*(*it).my_particle_kind_iter).atom_kind_name 
          &lt;&lt; std::endl
          &lt;&lt; "x: " &lt;&lt; (*it).position.x 
          &lt;&lt; " y: " &lt;&lt; (*it).position.y
      #ifdef USE_3D_GEOM
          &lt;&lt; "z: " &lt;&lt; (*it).position.z
      #endif
          &lt;&lt; std::endl;
      }
  </code></pre>
  
  <p>I now get nasty segfaults...</p>
  
  <p>Not to put too much here, but I'm also getting segfaults when I tried to do this:</p>
  
  <pre><code>std::cout &lt;&lt; "Bond ATOMS : " 
            &lt;&lt; (*current_molecule).my_bonds[0].particle_1-&gt;name
            &lt;&lt; std::endl
  </code></pre>
  
  <p>Again, current_molecule is a pointer to a s_molecule structure, which contains arrays of structures, which in turn either directly have vars or are pointers.  I can't get these multiple layers of indirection to work.  Suggestions on fixing these segfaults.</p>
  
  <p>FYI I'm compiling on Linux Centos 5.4 with g++ and using a custom makefile system.</p>