Jump to content
  • Advertisement
Sign in to follow this  
_ArmuT_

Dynamic Array of Pointers

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++;
}




Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 : )

Share this post


Link to post
Share on other sites
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[ i ] = new Class2();
}

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

Share this post


Link to post
Share on other sites


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 ..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[ i ] = new Class2();
}

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


No.

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:


int n = 5;
Class2 **Array = new Class2 *[n];
int i;

try
{
for (i = 0; i < n; ++i)
Array = new Class2();
}
catch(...)
{
while (--i > 0)
delete Array;
delete [] Array;
throw;
}

while (--i > 0)
delete Array;
delete [] Array;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!