Jump to content
  • Advertisement
Sign in to follow this  
hydrogen

Boost multi_array

This topic is 4102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I want is a multi_array as a class member which I initialize in the constructor. Unfortunately when I attempt to do this the program crashes immediately.
class CTerrainMap
{
private:
boost::multi_array<float, 2> heightmap;

int m_ns; //north-south map size
int m_ew; //east-west map size
public:
CTerrainMap(int ns,int ew) : m_ns(ns),m_ew(ew)
{
  heightmap = boost::multi_array<float, 2>(boost::extents[m_ns][m_ew]);
}
};

Probably something to do with it making a copy of the multi_array on the local stack. But if I cannot do it this way, how can I do it? I can get it to work if the member variable is a pointer to a multi_array boost::multi_array<float, 2>* heightmap; heightmap = new boost::multi_array<float, 2>(boost::extents[m_ns][m_ew]); But then I have to access array elements using (*heightmap)[3][5] Couldn't find a solution on google or searching the forums..

Share this post


Link to post
Share on other sites
Advertisement
CTerrainMap(int ns,int ew) : m_ns(ns),m_ew(ew),heightmap(boost::extents[ns][ew]
{
}
};

I'm not sure if that will work, but maybe something like that might work.

Share this post


Link to post
Share on other sites
You are right this does work, thanks, but I am still interested if there is a way to initialize the multi_array in the constructor body so that it will work like it does when in the initialization list.

Share this post


Link to post
Share on other sites
A boost::multi_array does have modifier functions, such as resize() and reshape(), that might suit your needs better than a simple assignment.

Share this post


Link to post
Share on other sites
I also just noticed on boost::multi_array documentation for operator = that a precondition is that:
std::equal(this->shape(),this->shape()+this->num_dimensions(),x.shape());

Thus, the array target needs to be the same shape as the source. The assignment doesn't really assign the overall source to the target, just the source elements. So you were getting the crash because the default-constructed array was a different shape than the temporary array on the stack.

Share this post


Link to post
Share on other sites
Quote:
Original post by hydrogen
You are right this does work, thanks, but I am still interested if there is a way to initialize the multi_array in the constructor body so that it will work like it does when in the initialization list.

You can't initialize the object in the constructor body. Initialization only happens once, and it happens before the body is entered. The array will be initialized by its default constructor.

Why don't you want to use initialization lists? You should really be using them if you can. See this page for more info.

[Edited by - MumbleFuzz on March 4, 2007 5:44:38 PM]

Share this post


Link to post
Share on other sites
Thanks, initialization list works fine. Agony's explaination explains why the initialization in the body failed. I just wanted to make sure there wasn't a common initialization method I didn't know about that would allow me the initialize it in the body (well re-initialize it).

Share this post


Link to post
Share on other sites
Quote:
Original post by hydrogen
Thanks, initialization list works fine. Agony's explaination explains why the initialization in the body failed. I just wanted to make sure there wasn't a common initialization method I didn't know about that would allow me the initialize it in the body (well re-initialize it).


The point was, there is *no* "initialization method", common or not, that allows you to initialize *anything* in the constructor body, because that's not what initialization is. The body is additional setup code that is called to initialize *the object*; initialization of *each member* is a separate concept, which is handled using the initialization list if at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!