Deleting a pointer array in C++

Started by
9 comments, last by MajinMusashi 19 years, 5 months ago
Suppose this situation:

C_Container *containerList[3];

containerList[0] = new C_Container( true,  0.0f, 5.0f, 1.0f );
containerList[1] = new C_Container( false, 5.0f, 5.0f, 1.0f );
containerList[2] = new C_Container( true, -4.0f,-2.0f, 1.0f );

for ( contNr = 0; contNr < NUMCONTS; ++contNr ) {
    delete containerList[contNr];
}

Is there another way to deallocate memory from "containerList"? Better yet, is there a better way to do all the job described above (create and initialize the array with containers)? Thanks!!
Advertisement
its delete [] containerList;
(i think)

That looks very strange because you could probably just do

C_Container *containerList = new C_Container[3];
C_Container[0] = C_Container( , , ,);
C_Container[1] = C_Container( , , ,);
C_Container[2] = C_Container( , , ,);

then delete it with
delete [] containerList;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
Quote:Original post by SumDude
its delete [] containerList;
(i think)

That looks very strange because you could probably just do

C_Container *containerList = new C_Container[3];
C_Container[0] = C_Container( , , ,);
C_Container[1] = C_Container( , , ,);
C_Container[2] = C_Container( , , ,);

then delete it with
delete [] containerList;

I'm confused by his initial declaration [I can never figure out how *s and [] and & and such work together], but given the way he's declared the individual elements, he has to delete them individually just the way he has. I'm pretty certain he's declared an array of C_Container pointers, so it should all be kosher.

The best alternative would be to just switch to the STL's vector.

std::vector<C_Container> containerList;containerList.push_back(C_Container( true,  0.0f, 5.0f, 1.0f ));containerList.push_back(C_Container( false, 5.0f, 5.0f, 1.0f ));containerList.push_back(C_Container( true, -4.0f,-2.0f, 1.0f ));

Then, no deletions are neccessary at all.

CM
Quote:Original post by SumDude
its delete [] containerList;
(i think)


I dont't do that way because VC++ throws me a "Warning C4154: deletion of an array expression; conversion to pointer supplied".

Quote:That looks very strange because you could probably just do

C_Container *containerList = new C_Container[3];
C_Container[0] = C_Container( , , ,);
C_Container[1] = C_Container( , , ,);
C_Container[2] = C_Container( , , ,);

then delete it with
delete [] containerList;


Hummmmm... I think it works, but this way I must create a empty body constructor just for the first statement. And seems to me that there is a object copy in "C_Container[0] = C_Container( , , ,);" that can be avoided using pointers.

Thanks, SumDude!!!
Quote:Original post by Conner McCloud
I'm confused by his initial declaration [I can never figure out how *s and [] and & and such work together], but given the way he's declared the individual elements, he has to delete them individually just the way he has. I'm pretty certain he's declared an array of C_Container pointers, so it should all be kosher.

Yes, exactly that, an array of C_Container's.

Quote:The best alternative would be to just switch to the STL's vector. Then, no deletions are neccessary at all.

Very interesting, Conner, but I'm more interested, at the moment, in discovering the basics behind C++ (like you, I can never figure out how *s and [] and & work together :) ), so a non-STL answer would be better.

Thanks a lot!!!
Why would you want to avoid it?

Overall it looks nicer than using a for statement to delete the allocated memory and i'm probably sure it would run faster too since you don't need the for loop.

Besides, your using a constructor for doing each containerList[] = new C_Container().

BTW: Conner, its a good idea you clear/empty the vector when your done with it :).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
Quote:Original post by SumDude
Why would you want to avoid it?

If it is a warning, there is something that is not completely ok, right? And I don't like warnings popping out all the time :)

Quote:Overall it looks nicer than using a for statement to delete the allocated memory and i'm probably sure it would run faster too since you don't need the for loop.

Yes, I must admit it, it would be faster.

Quote:
Besides, your using a constructor for doing each containerList[] = new C_Container().

Yes, I'm using a C_Container( bool, float, float, float ), and not a C_Container(). I don't want to create a empty body ctor just because of that list.

Quote:BTW: Conner, its a good idea you clear/empty the vector when your done with it :).

Agreed [wink]

Thanks, SumDude!!!
Quote:Original post by SumDude
would

C_Container *containerList;

containerList[0] = C_Container( )
containerList[1] = C_Container( )
containerList[2] = C_Container( )

work? Don't need to delete as well


No!! That would be writing to random memory!!!
Why are you trying to avoid using an empty constructor?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
SumDude, you spread some serious misinformation.
C_Container *containerList[3];

The above is an array of pointers on the stack, which you cannot delete!
C_Container *containerList[3];// baddelete [] containerList;C_Container *containerList2 = new C_Container[3];// gooddelete [] containerList2;

Yet the above refers to two fundamentally different concepts.
The stack based array allows for individual construction of its elements (e.g. using a non-trivial constructor), while the dynamic array can only be created if C_Container provides a default constructor taking an empty argument list.

I wrote the following in some other thread: you can only (safely) delete what you created with new (same goes for delete [] and new []).
It's not that hard, isn't it [wink]?

This topic is closed to new replies.

Advertisement