user3111197
user3111197

Reputation: 83

Trying to use boost::multi_array of pointer of object

I face the following problem. I want to create a multidimensional array of pointers of an object use boost::multi_array, but even though the code I write compiles, when I try to run in Eclipse the program is terminated and nothing is printed. Let me illustrate a very little example in case this could be of any help. So having the following very small simple class:

class example {
     public:
            example();
            virtual ~example();

            int a;

};

I just try to create and use a multi_array of pointers of this class in the following way:

int main() {

           typedef boost::multi_array<example * , 2> array_type1;

           array_type1 DE(boost::extents[2][2]);

           DE[0][0]->a=6;
           DE[1][0]->a=7;
           DE[0][1]->a=8;
           DE[1][1]->a=9;

cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;

}

Note as well that when I run the same code using the boost/test/minimal.hpp (http://www.boost.org/doc/libs/1_46_1/libs/test/doc/html/minimal.html) to do a check of what is going on and as a result the main looks like this:

int test_main(int, char*[]){


           typedef boost::multi_array<example * , 2> array_type1;

           array_type1 DE(boost::extents[2][2]);

           DE[0][0]->a=6;
           DE[1][0]->a=7;
           DE[0][1]->a=8;
           DE[1][1]->a=9;

cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return boost::exit_success;

}

I receive the following message:

/usr/include/boost/test/minimal.hpp(123): exception "memory access violation at address: 0x00000008: no mapping at fault address" caught in function: 'int main(int, char**)'

**** Testing aborted.
**** 1 error detected

Any suggestions on how to resolve this would be very much helpful to me right now!

Upvotes: 0

Views: 166

Answers (1)

Quentin
Quentin

Reputation: 63174

array_type1 DE(boost::extents[2][2]);
DE[0][0]->a=6;

You dereference the pointer at DE[0][0], but never made it point to an actual example instance beforehand.

Upvotes: 1

Related Questions