Tuesday, 15 January 2013

c++ - Problems with shared_ptr wrapping a dynamic array -



c++ - Problems with shared_ptr<T[]> wrapping a dynamic array -

i wanted replace raw pointers in class std::shared_ptr don't have worry when create copies of class. raw pointers point dynamic array. using shared_ptr dynamic arrays possible when give custom deleter, e. g. default_delete<t[]>.

but big error list seek assign new value field, on construction.

here's minimal code sample:

#include <memory> #include <cstddef> using namespace std; template<typename t> shared_ptr<t[]> make_shared_array(size_t size) { homecoming shared_ptr<t[]>(new t[size], default_delete<t[]>()); } struct foo { shared_ptr<char[]> field; }; int main() { foo a; // line produces error. a.field = make_shared_array<char>(256); homecoming 0; }

nb: yes, know could/should vector instead of dynamic arrays. performance not same. heavy image processing , arrays hold pixels. on less vga resolution processing time increased 8 11 s. that's quite lot.

update: of course of study can provide errors here. didn't know if should clutter problem description it. here is:

c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(754) : error c2664: 'std::_ptr_base<_ty>::_reset0' : cannot convert parameter 1 'char ' 'char ()[]' with [ _ty=char [] ] types pointed unrelated; conversion requires reinterpret_cast, c-style cast or function-style cast c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(723) : see reference function template instantiation 'void std::shared_ptr<_ty>::_resetp0<_ux>(_ux *,std::_ref_count_base *)' beingness compiled with [ _ty=char [], _ux=char ] c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(723) : see reference function template instantiation 'void std::shared_ptr<_ty>::_resetp0<_ux>(_ux *,std::_ref_count_base *)' beingness compiled with [ _ty=char [], _ux=char ] c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(494) : see reference function template instantiation 'void std::shared_ptr<_ty>::_resetp<_ux,_dx>(_ux *,_dx)' beingness compiled with [ _ty=char [], _ux=char, _dx=std::default_delete ] c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(494) : see reference function template instantiation 'void std::shared_ptr<_ty>::_resetp<_ux,_dx>(_ux *,_dx)' beingness compiled with [ _ty=char [], _ux=char, _dx=std::default_delete ] problem.cpp(9) : see reference function template instantiation 'std::shared_ptr<_ty>::shared_ptr>(_ux *,_dx)' beingness compiled with [ _ty=char [], t=char, _ux=char, _dx=std::default_delete ] problem.cpp(9) : see reference function template instantiation 'std::shared_ptr<_ty>::shared_ptr>(_ux *,_dx)' beingness compiled with [ _ty=char [], t=char, _ux=char, _dx=std::default_delete ] problem.cpp(21) : see reference function template instantiation 'std::shared_ptr<_ty> make_shared_array(size_t)' beingness compiled with [ _ty=char [] ]

the solution suggest possible, lose size of array:

#include <memory> #include <cstddef> using namespace std; template<typename t> shared_ptr<t> make_shared_array(size_t size) { homecoming shared_ptr<t>(new t[size], default_delete<t[]>()); } struct foo { shared_ptr<char> field; }; int main() { foo a; a.field = make_shared_array<char>(256); homecoming 0; }

what have done here allow array decay pointer. long advertisement deleter array deleter should behave correctly.

to prevent loss of size, , if cannot utilize boost::shared_array suggested, suggest encapsulate info in own shared_array class.

c++ c++11 shared-ptr

No comments:

Post a Comment