C++ vector being overwritten
<p>I have I have objects with x, y, z coordinates and another parameter - energy. The energy of objects with the same x, y, z coordinates are to be summed up. I have seen a program how this can be implemeted and followed the exact same approach, however, my program fails and I do not understand why.</p>

<p>Outlined is the approach.</p>

<p>I have a vector of the object of class D;</p>

<pre><code>class D; //for the sake of time you will understand the structure of this class as u keep reading

int main()
 {
  vector&lt; D&gt;cont; //the objects to be summed are stored in this container
  vector&lt; D&gt;cont2; //Second container: you will see the need later on

  co_ord [100][100][100]; //Declare the range of available x, and z coordinates

  for (int z = -49; z&lt;50; z++)
      for (int y = -49; y&lt;50; y++)
          for (int x = -49; x&lt;50; x++)
              co_ord[z][y][x] = -1;

 for (int i = 0; i &lt; cont.size(); i++){
  int x, y, z, E;
 **//PROBLEM AREA!!!**
   x = cont[i].getx();//member function of class D : returns x of current obj
   y = cont[i].gety();
   z = cont[i].gez();
   E = cont[i].getE();
 if(x&gt;=50){x = 49;}
 if(y&gt;=50){y = 49;}
if(z&gt;=50){z = 49;}

  if(x&lt;= -50){x = -49;}
  if(y&lt;= -50){y = -49;}
 if(z&lt;= -50){z = -49;}

  if (co_ord[z][y][x] = -1)
  {
    D obj(x, y, z, E); //create new object of class D
    cont2.push_back(obj);
    int index = cont2.size();
    co_ord[z][y][x] = index - 1; // co_ord[z][y][x] has the storage index of this co-ordinate
  } 
 else {cont2[co_ord[z][y][x]].AddEn(E); //AddEn() is a member funtion of class D that adds energy to "this" obj;    
  return 0;
   }
}
</code></pre>

<p>}</p>

<p>Now I have 2 major problems:</p>

<ol>
<li><p>If I use co_ord[300][300][300], the program gives a <strong>segmentation fault</strong>. A <strong>cout</strong> statement will execute if placed before the 3 consecutive for-loops, but not afterwards;
When I reduce the dimension of co_ord to co_ord[100][100][100], that problem is solved and I will be able to cout a statement anywhere else. </p></li>
<li><p>However, there is another weird problem that I still have no idea of why that is. The area where this occurs is labeled "PROBLEM AREA" in the preceeding code. Basically what happens is that co_ord[z][y][x] will give out some strange values after the first loop. The first loop runs well i.e co_ord[-9][0][1] will cout -1, but on the second loop, I have values like 6783 and other sometimes maybe up to an 8 digit number. But this is occuring during the second loop. I have tried everything I know but I am not succeeding.</p>

<p>Please, can someone offer a clue on why I have the 2 above mentioned errors or the way around it? As I said, I have exactly a similar code running on my system which is part of a larger application. In the code, they implemented some memory allocation procedure for the container class they created, however their procedure is complex for me hence my decision to do it with the standard c++ library vector. Any help will be appreciated.</p></li>
</ol>

<p>Charles</p>

<p>sorry for posting an erroneous code earlier, i have corrected most (I hope all) of the typos</p>
