Hi, I am new to boost, and is trying to implement shared_ptr solution to a 2d array of pointers. Eg Character *** characters; characters = new Character**[_info._l ]; for (int i=0; i< _info._l; i++){ characters[i] = new Character* [_info._w]; for( int j=0;j < _info._w; j++){ characters[i][j] = 0; } } This is a sparse array, most of the pointers will be set to 0. As I could not find much information about shared_array on line, I would like to check is there any issue with this implementation. boost::shared_array<boost::shared_array<boost::shared_ptr<Character>>> characterGrid(new boost::shared_array<boost::shared_ptr<Character>>[_info._l ]); for( int i=0;i< _info._l; i++) { characterGrid[i] = boost::shared_array<boost::shared_ptr<Character>>(new boost::shared_ptr<Character>[_info._w]); for( int j=0; j< _info._w; j++) { //is it alright for me to use an empty shared_ptr in this context to init the array? characterGrid[i][j] = boost::shared_ptr<Character>(); } } Thanks. Jeffrey.
Do you really need to have a shared_array containing shared_ptrs? You would probably be better off with something like: std::vector< std::vector< boost::shared_ptr<Character> > > characterGrid; characterGrid.resize( _info.l ); for( int i = 0; i < _info.l; ++i ) characterGrid.resize( _info.w ); James On 8/30/07, Jeffrey Jiang <gamedboy@singnet.com.sg> wrote:
This is a sparse array, most of the pointers will be set to 0. As I could not find much information about shared_array on line, I would like to check is there any issue with this implementation.
boost::shared_array<boost::shared_array<boost::shared_ptr<Character>>> characterGrid(new boost::shared_array<boost::shared_ptr<Character>>[_info._l ]);
for( int i=0;i< _info._l; i++) {
characterGrid[i] = boost::shared_array<boost::shared_ptr<Character>>(new boost::shared_ptr<Character>[_info._w]);
for( int j=0; j< _info._w; j++) {
//is it alright for me to use an empty shared_ptr in this context to init the array?
characterGrid[i][j] = boost::shared_ptr<Character>();
}
}
Thanks for the reply. I agree that this way is prob clearer and predictable. However in this case I am implementing a fix size grid of pointers, to represent a geographical map, and the location of the pointer to determine the position of the character. So although using vector in this case would work, it will convey a wrong message to the users that -they can resize the grid -the location of the pointer is not impt I would rather use a shared_ptr<Character> **characterGrid instead, which bring us back to the problem below. Jeffrey. "James Emerton" <ephelon@gmail.com> wrote in message news:c9424d940708310855t3d5acd1boef1c2686bfabfca5@mail.gmail.com...
Do you really need to have a shared_array containing shared_ptrs?
You would probably be better off with something like:
std::vector< std::vector< boost::shared_ptr<Character> > > characterGrid; characterGrid.resize( _info.l ); for( int i = 0; i < _info.l; ++i ) characterGrid.resize( _info.w );
James
On 8/30/07, Jeffrey Jiang <gamedboy@singnet.com.sg> wrote:
This is a sparse array, most of the pointers will be set to 0. As I could not find much information about shared_array on line, I would like to check is there any issue with this implementation.
boost::shared_array<boost::shared_array<boost::shared_ptr<Character>>> characterGrid(new boost::shared_array<boost::shared_ptr<Character>>[_info._l ]);
for( int i=0;i< _info._l; i++) {
characterGrid[i] = boost::shared_array<boost::shared_ptr<Character>>(new boost::shared_ptr<Character>[_info._w]);
for( int j=0; j< _info._w; j++) {
//is it alright for me to use an empty shared_ptr in this context to init the array?
characterGrid[i][j] = boost::shared_ptr<Character>();
}
}
On 8/31/07, Jeffrey Jiang <gamedboy@singnet.com.sg> wrote:
I would rather use a shared_ptr<Character> **characterGrid instead, which bring us back to the problem below.
The problem with your approach is that it depends on dynamically allocating objects that were not meant to be allocated dynamically. The boost::shared_* types are meant to be created on the stack and passed by value. When you start nesting them like that you're going to end up with very confusing deref statements. Technically, I suppose it's feasible. If you really must attempt it, use the typedef statement to make things look a little more sane. James
participants (2)
-
James Emerton
-
Jeffrey Jiang