Dynamic Array of Pointers

Started by
10 comments, last by ToohrVyk 16 years, 3 months ago
Hi folks, For the last half an hour, I am staring at my IDE and trying to figure out how to allocate an array of pointers to an another class from a class again. Would somebody give me a pointer ( without use of vectors ) please ? During this half an hour, All I can come up with is to drop the idea of declaring an array of pointers but sticking to a cursor pointer to mark the position of my data; What I mean is smtg like in a constructor:

classA::classA (int iCount ) {
for (int i=1;i<=iCount;i++) {	
     m_pSuit = new classB(i);
     m_pSuit++;
}




Advertisement
Dunno why not the use of vectors but here...

//I think that is right...might need one less * on the Class**Class** array=new Class*[5];
------------George Gough
I think you need to be a little clearer on what you want:
Do you want one class to define an array of another?

I would really, really recommend vectors, but ill help you either way ;)
Thank you guys,
I guess the pointer to a pointer approach will work.
BTW; How can I delete an array on the heap, in this case
a one that is using pointer to a pointer , again with
delete[] as we do for arrays ?

And I can't use vectors because I haven't learned it yet but
I will be there hopefully ; )

Thanks again!
Quote:Original post by ender7771
I think you need to be a little clearer on what you want:
Do you want one class to define an array of another?



What I am trying to do is :
one class to define a dynamic array of another : )

Quote:Original post by _ArmuT_
Quote:Original post by ender7771
I think you need to be a little clearer on what you want:
Do you want one class to define an array of another?



What I am trying to do is :
one class to define a dynamic array of another : )


So you want to create an array of pointers to objects?

     int n = 5;     // first create the array of pointers     Class2 ** Array = new Class2 *[ n ];     for ( int i = 0; i < n; i++ ) {     // then create an object for each element in the array          Array = new Class2();<br>     }<br><br>     // be sure to delete both the objects and the array itself<br>     for ( int j = 0; j &lt; n; j++ ) {<br>         delete Array[ j ];<br>     }<br>     delete [] Array;<br></pre>


Quote:

So you want to create an array of pointers to objects?

     // be sure to delete both the objects and the array itself     for ( int j = 0; j < n; j++ ) {         delete Array[ j ];     }     delete [] Array;




What you are saying is I have to delete every object stored in my array
manually and also I have to delete the array itself ( double pointer) in
order to prevent memory leak ?

Thanks for your time ..

Quote:Original post by _ArmuT_


Quote:

So you want to create an array of pointers to objects?

     // be sure to delete both the objects and the array itself     for ( int j = 0; j < n; j++ ) {         delete Array[ j ];     }     delete [] Array;




What you are saying is I have to delete every object stored in my array
manually and also I have to delete the array itself ( double pointer) in
order to prevent memory leak ?

Thanks for your time ..


Yep. Also, be sure to use delete[] Array to delect the array (not delete Array), or bad things will happen.

Quote:Original post by _ArmuT_


What you are saying is I have to delete every object stored in my array
manually and also I have to delete the array itself ( double pointer) in
order to prevent memory leak ?



Yes sir, which is why everyone here is strongly suggesting that you use std::vector and spare yourself the trouble. Or at the very least, you should consider using some of the smart pointer classes found in the boost library.

Quote:Original post by small_black_sun
So you want to create an array of pointers to objects?

     int n = 5;     // first create the array of pointers     Class2 ** Array = new Class2 *[ n ];     for ( int i = 0; i < n; i++ ) {     // then create an object for each element in the array          Array = new Class2();<br>     }<br><br>     // be sure to delete both the objects and the array itself<br>     for ( int j = 0; j &lt; n; j++ ) {<br>         delete Array[ j ];<br>     }<br>     delete [] Array;<br></pre><!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br><b>No.</b><br><br>If any of the constructors or allocation functions in your code throw an exception, you're going to leak a lot of memory. At the very least:<br><br><pre><br>int n = 5;<br>Class2 **Array = new Class2 *[n];<br>int i;<br><br>try <br>{<br>  for (i = 0; i &lt; n; ++i)<br>    Array<span style="font-weight:bold;"> = new Class2();<br>}<br>catch(…)<br>{<br>  while (–i &gt; 0) <br>    delete Array<span style="font-weight:bold;">;<br>  delete [] Array;<br>  throw;<br>}<br><br>while (–i &gt; 0) <br>  delete Array<span style="font-weight:bold;">;<br>delete [] Array;<br></pre><br><br>

This topic is closed to new replies.

Advertisement