C++ Dynamic Array of Pointers

Started by
5 comments, last by Zahlman 17 years, 1 month ago
How does one create a pointer to a dynamic array of pointers in C++? For example, say I have two Monster pointers:

Monster* monster = new Monster();
Monster* monster2 = new Monster();
If I try to make a normal dynamic array of monsters, like so:

Monster* monsters = new Monster[2];
And then try to assign those pointers to it, like so:

monsters[0] = monster;
monsters[1] = monster2;
I receive the error "error C2440: '=' : cannot convert from 'Monster *' to 'Monster'". Which part of this is wrong?
Advertisement
The problem with your code is that you have a pointer to an array. That pointer points to an array of Monster -- you can't put pointers into it. What you need is a pointer to an array of pointers, so you'd declare your array as:

Monster **monsters = new Monster*[size];


However, this is reinventing the wheel. This is a perfect place to use a vector. A vector is a STL container class that can be dynamically resized and is very fast when adding things and removing things from the end (although anything can be inserted/deleted from any place inside).

This, for example, creates an array adds 10 integers to it numbered 1 through 10.

#include <vector>int main(){    std::vector<int> integers;   for(int i = 0; i < 10; i++)    integers.push_back(i);}


If you are particulary perceptive, you might begin to notice that using this implementation has a flaw. When removing pointer elements, the data is not deleted. Thus you've got a memory leak. To fix this, you can either use the boost ptr_vector or wrap the vector with another class that deletes the data (for pointers). However, I'd question as to whether you'd need to use pointers at all with vectors (you might, it depends on what you're doing with the data, though if your array owns the data, then you probably don't need pointers).
You've created an array of values not an array of pointers. The syntax for an array of monster pointers is:

Monster **monsters = new Monster*[2];//but then you need to allocate the monsters:for ( int i = 0; i < 2; ++i ){    monster = new Monster();}//don't forget to free all the memory when you're done:for ( int i = 0; i < 2; ++i ){    delete monster;}delete[] monsters;



or to just use your code more "as written"
Monster* monster = new Monster();Monster* monster2 = new Monster();Monster** monsters = new Monster*[2];monsters[0] = monster;monsters[1] = monster2;//again don't forget to delete all the memory (just use the code in the other section)


or use std::vector. but it's good to know how to do it the "old fashioned" way.

-me
Thank you both for your help. I know about vectors, but the reason I was doing it this way was because in my actual code I was actually going to use the array in another function outside the one that created it, so I figured I needed to allocate memory manually so that the array didn't get deleted once the function returned.

Is there a way to make sure a vector isn't deleted once the function that created it returns?
You can always return a vector or accept a vector passed by reference to the function.
If you are just storing pointers you might want to use boost::ptr_vector instead. ptr_vector will work much like vector, but easier to work with for pointers, and it also handles cleanup (with vector you will have to loop through and call delete on each item yourself)
Quote:Original post by Mortarion
Is there a way to make sure a vector isn't deleted once the function that created it returns?


vectors aren't "deleted"; they are destructed. 'delete' only happens explicitly, and only on pointers. But anyway, you can "hold onto" a vector after the current function ends in exactly the same way you would do with an int: either return it from the function, or (not without a good reason!) assign it to a parameter which is a vector (of the same element type) passed by (non-const) reference. (It's more usual to just modify the passed-in vector instead of creating a local one at all, but even then, you should normally *prefer* to use the return value to communicate "back" from a function.) This is because they aren't pointers - whereas the thing you get back from 'new' is indeed a pointer.

This topic is closed to new replies.

Advertisement