Deleting a pointer array in C++

Recommended Posts

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

Share on other sites
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;

Share on other sites
Quote:
 Original post by SumDudeits delete [] containerList;(i think)That looks very strange because you could probably just doC_Container *containerList = new C_Container[3];C_Container[0] = C_Container( , , ,);C_Container[1] = C_Container( , , ,);C_Container[2] = C_Container( , , ,);then delete it withdelete [] 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

Share on other sites
Quote:
 Original post by SumDudeits 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 doC_Container *containerList = new C_Container[3];C_Container[0] = C_Container( , , ,);C_Container[1] = C_Container( , , ,);C_Container[2] = C_Container( , , ,);then delete it withdelete [] 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!!!

Share on other sites
Quote:
 Original post by Conner McCloudI'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!!!

Share on other sites
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 :).

Share on other sites
Quote:
 Original post by SumDudeWhy 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]

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

Agreed [wink]

Thanks, SumDude!!!

Share on other sites
Quote:
 Original post by SumDudewouldC_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!!!

Share on other sites
Why are you trying to avoid using an empty constructor?

Share on other sites
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]?

Share on other sites
Quote:
 Original post by SumDudeWhy are you trying to avoid using an empty constructor?

Because that way, I would end up creating an additional constructor, and copying objects to the array (instead of referencing them, what is slower). Am I right?

PS.: sorry for the bad english :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11