Re: [Boost-users] Error (runtime) assigning multi_array to multi_array with smaller dimensions

//at class constructor boost::multi_array<char, 2> array2d = new boost::multi_array<char, 2>(boost::extents[x][y]);
//at class method boost::multi_array<char, 1> array1d = array2d[0];
I tried out those two lines (minus the "new") here (see below), and they compile without complaints in g++ 4.1.2. Yung-Chin -- contents of test file -- #include <iostream> #include <boost/multi_array.hpp> int main() { size_t x = 5; size_t y = 7; boost::multi_array<char, 2> array2d = boost::multi_array<char, 2>(boost::extents[x][y]); std::cout << array2d.num_dimensions() << std::endl; std::cout << array2d[0].num_dimensions() << std::endl; boost::multi_array<char, 1> array1d = array2d[0]; std::cout << array1d.num_dimensions() << std::endl; return 0; } //main

Thanx for the reply Yung-Chin. It compiles ok, what i get is a run time error. In a single main file, it works fine too. But i had trouble when declaring the multi_array in the header file, initializing it on the construtor and then trying to assign first position to a one dimension array in one method. It is something like this: //class header file boost::multi_array<char, 2> array2d; //class cpp file //at class constructor array2d = boost::multi_array<char,2>(boost::extents[x][y]); //at class method boost::multi_array<char, 1> array1d = array2d[0]; then it compiles fine. But at runtime i get the error: Reference boost::detail::multi_array::value_accessor_n<T, NumDims>::access(boost::type<Reference>, boost::multi_array_types::index, TPtr, const boost::multi_array_types::size_type*, const boost::multi_array_types::index*, const boost::multi_array_types::index*) const [with Reference = boost::detail::multi_array::sub_array<char, 1u>, TPtr = char*, T = char, unsigned int NumDims = 2u]: Assertion `size_type(idx - index_bases[0]) < extents[0]' failed. If you have an idea of what's going on, I'd be glad to know! On Thu, May 21, 2009 at 6:22 PM, Oei, YC <oei.yungchin@gmail.com> wrote:
//at class constructor boost::multi_array<char, 2> array2d = new boost::multi_array<char, 2>(boost::extents[x][y]);
//at class method boost::multi_array<char, 1> array1d = array2d[0];
I tried out those two lines (minus the "new") here (see below), and they compile without complaints in g++ 4.1.2.
Yung-Chin
-- contents of test file --
#include <iostream> #include <boost/multi_array.hpp>
int main() { size_t x = 5; size_t y = 7;
boost::multi_array<char, 2> array2d = boost::multi_array<char, 2>(boost::extents[x][y]); std::cout << array2d.num_dimensions() << std::endl; std::cout << array2d[0].num_dimensions() << std::endl;
boost::multi_array<char, 1> array1d = array2d[0]; std::cout << array1d.num_dimensions() << std::endl; return 0; } //main _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Matheus Araújo Aguiar Computer Scientist matheus.pit@gmail.com

//class header file boost::multi_array<char, 2> array2d;
//class cpp file //at class constructor array2d = boost::multi_array<char,2>(boost::extents[x][y]);
I think the problem is that the last line quoted above is an assignment, not an initialisation: if you look in the boost headers, you'll see the assignment operator is very different from the copy constructor. Basically if you have a constructor that first default-constructs array2d, then you'll have an empty array. Then assigning to it, means you are trying a deep-copy from a x-by-y array to a 0-by-0 array. One way to fix it is to add a line: //at class constructor array2d.resize(boost::extents[x][y]); array2d = boost::multi_array<char,2>(boost::extents[x][y]); Otherwise, you can initialise array2d before the constructor body. Yung-Chin

Apologies all - was a bit too eagerly hitting send. So, quoting myself a bit:
//class cpp file //at class constructor array2d = boost::multi_array<char,2>(boost::extents[x][y]);
I think the problem is that the last line quoted above is an assignment, not an initialisation:
Actually if this was really the problem, the assertions in the assignment operator should cause the construction to fail. But the failed assertion you sent (below) does suggest that array2d is for some reason empty. So I'm a bit puzzled it gets that far?
Reference boost::detail::multi_array::value_accessor_n<T, NumDims>::access(boost::type<Reference>, boost::multi_array_types::index, TPtr, const boost::multi_array_types::size_type*, const boost::multi_array_types::index*, const boost::multi_array_types::index*) const [with Reference = boost::detail::multi_array::sub_array<char, 1u>, TPtr = char*, T = char, unsigned int NumDims = 2u]: Assertion `size_type(idx - index_bases[0]) < extents[0]' failed.
participants (2)
-
Matheus Araújo Aguiar
-
Oei, YC