Hi,
Currently I am working on a little project, where I'm facing a problem concerning the lifetime of dynamically allocated boost-vectors. At least I think this is where the problem lies.
I've broken down my code to a minimum, to show you, what the problem is about.
So, what should my code do?
I made a class, which holds two functions, the constructor and a pointer to a multi-dimensional array.
Within the constructor, the multi-dim array is created.
1 2
|
typedef boost::multi_array<p_vec, 2> array_type2d;
typedef array_type2d::index index2;
|
The elements of the multi-dim array are pointers to boost::numeric::ublas::vector<double> (called p_vec).
1 2
|
using namespace boost::numeric::ublas;
typedef vector<double>* p_vec;
|
This works, but when i want to access this vector from a second function i get
[1](0)
[1](1)
[1](2)
[1](1)
[1](2)
[1](3)
[1](2)
[1](3)
[1](4)
sandbox: /usr/include/boost/multi_array/base.hpp:177: Reference boost::detail::m
ulti_array::value_accessor_one<T>::access(boost::type<Reference>, boost::multi_a
rray_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::numeric::ublas::vector<double, boost::numeric::ublas::unboun
ded_array<double, std::allocator<double> > >*&, TPtr = boost::numeric::ublas::ve
ctor<double, boost::numeric::ublas::unbounded_array<double, std::allocator<doubl
e> > >**, T = boost::numeric::ublas::vector<double, boost::numeric::ublas::unbou
nded_array<double, std::allocator<double> > >*]: Assertion `idx - index_bases[0]
>= 0' failed.
|
The first part of the output simply shows the dynamically created vector<double> (first integer represents the size of the vector, which is one, and the second double is the value, which is stored).
This is my code:
main.cpp:
1 2 3 4 5 6 7
|
#include "test_cpu1_2.hpp"
int main(){
cell_method cell1;
cell1.interactions();
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/multi_array.hpp>
#include <stdio.h>
using namespace boost::numeric::ublas;
typedef vector<double>* p_vec;
typedef boost::multi_array<p_vec, 2> array_type2d;
typedef array_type2d::index index2;
class cell_method
{
public:
cell_method(void);
void create_cells(void);
void interactions();
array_type2d* pcell_2d;
};
cell_method::cell_method(){
array_type2d cell_2d(boost::extents[3][3]);
pcell_2d = &cell_2d;
create_cells();
};
void cell_method::create_cells(){
for(int i = 0; i < 3;++i){
for(int j = 0; j < 3; ++j){
p_vec p_vec2 = new vector<double>;
(*pcell_2d)[i][j] = p_vec2;
(*(*pcell_2d)[i][j]).resize(1);
(*(*pcell_2d)[i][j])(0) = i + j;
std::cout << (*(*pcell_2d)[i][j]) << std::endl;
}
}
};
void cell_method::interactions(){
std::cout << (*(*pcell_2d)[1][1]) << std::endl;
}
|
regards,
juergen