Archived

This topic is now archived and is closed to further replies.

yanuart

Pointer to pointer aka **

Recommended Posts

Do u think using ** to make an array of pointer is safe ??? Can it cause problems later on ?? Edited by - yanuart on December 31, 2001 10:30:00 AM

Share this post


Link to post
Share on other sites
In itself, its fine. Ive used it many times in the past. The hardest part is to make sure your deleting all of the allocated memory. Ive tracked a lot of leaks to poorly maintained arrays of pointers, so much in fact that Ive started using the Standard Template Library to maintain my arrays. It seems to work better for me.

N.

Share this post


Link to post
Share on other sites
Well what I wanna do is to make an array of pointer to an object, the array size can be quite huge though.
Using ** is very convinient for me because I don''t have to set up the size of the array and yet I can use array style coding to get my pointer.
Are u sure it''s safe ?? the biggest problem that i worried the most is that i don''t have to set up the size of the array ?? don''t u think it''ll someday ruin everything ??

Share this post


Link to post
Share on other sites
How are you adding elements to your array.

If you are tracking a current count and then allocating a new array of old array length + 1, then copying the old array into the new locations for huge amounts of pointers can be very CPU cycle costly and more error prone, but do-able. Again, its just a matter of how you maintain it.

If its a pre-allocated array of pointers its only limited by system resources, and access will be fast.

But if its going to be a large unknown number of elements, I recommend using STL . that way you can constantly add new elements without worrying about memory copying or how big it will be, STL handles it all the dynamic allocation. And with the vector you can still access all your pointers via their index into that array (just slightly different) like this..

Soldier *mySoldier;
mySoldier = SoldierArray.at(5);

Share this post


Link to post
Share on other sites