Heap vs Stack allocation
<p>I am alittle bit confused on topic of allocating objects on heap vs allocating on stack, and when and how delete() should be called.</p>

<p>For example I have class Vector. I would like to make an array of these.</p>

<p>I could either do this</p>

<pre><code>Vector** v = new Vector*[100]; //create an array of 100 pointers to 100 Vector objects
</code></pre>

<p>This as I understand would allocate everything (well except pointer addresses) on a heap?
So to free memory I would need to:</p>

<pre><code>for (int i = 0; i &lt; 100; ++i)
{
   delete(v[i]);
}
delete(v);
</code></pre>

<p>OR just</p>

<pre><code>delete(v);
</code></pre>

<p>is enough?</p>

<p>Now another example:</p>

<pre><code>Vector* v = Vector[100];
</code></pre>

<p>Whats happening in this case? Where does allocation happen? Heap or stack?
Do I still need to call</p>

<pre><code>delete(v);
</code></pre>

<p>But this is not all question, sorry for long post..</p>

<p>example:</p>

<pre><code>class Vector
{
  int x, y, z;
}

Vector* v = new Vector();
</code></pre>

<p>wheres x, y, z allocated? Heap or stack?</p>

<p>or how about this:</p>

<pre><code>class Vector2
{
   int items[10];
}

Vector2* v2 = new Vector2();
</code></pre>

<p>where are items[10] allocated?
How do I delete v2? Do i need custom destructor?</p>

<p>Also last but not least how about this one:</p>

<pre><code>class Vector3
{
   int* items;
}

Vector3 v3 = Vector3();
</code></pre>

<p>Where is items pointer stored? heap or stack? How do I delete this?</p>

<p>Thanks and sorry for long question. Ive been having trouble with this stuff for long time and couldnt really find any complete explanation on line.</p>
