Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with shared_ptr<T[]> wrapping a dynamic array
    primarykey
    data
    text
    <p>I wanted to replace some raw pointers in my class with a <code>std::shared_ptr</code> so that I don't have to worry when I create copies of that class. But the raw pointers point to a dynamic array. Using a shared_ptr with dynamic arrays is possible when you give it a custom deleter, e.&thinsp;g. <code>default_delete&lt;T[]&gt;</code>.</p> <p>But I get a big error list as soon as I try to assign a new value to that field, even on construction.</p> <p>Here's a minimal code sample:</p> <pre><code>#include &lt;memory&gt; #include &lt;cstddef&gt; using namespace std; template&lt;typename T&gt; shared_ptr&lt;T[]&gt; make_shared_array(size_t size) { return shared_ptr&lt;T[]&gt;(new T[size], default_delete&lt;T[]&gt;()); } struct Foo { shared_ptr&lt;char[]&gt; field; }; int main() { Foo a; // This line produces the error. a.field = make_shared_array&lt;char&gt;(256); return 0; } </code></pre> <hr> <p><strong>NB:</strong> Yes, I know that I could/should <code>vector</code> instead of dynamic arrays. But their performance is not the same. I do some heavy image processing and the arrays hold the pixels. On less than VGA resolution the processing time increased from 8 to 11&thinsp;s. That's quite a lot.</p> <hr> <p><strong>Update:</strong> Of course I can provide the errors here. I just didn't know if I should clutter the problem description with it. But here it is:</p> <blockquote> <p>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(754) : error C2664: 'std::_Ptr_base&lt;_Ty>::_Reset0' : cannot convert parameter 1 from 'char <em>' to 'char (</em>)[]'<br> with<br> [<br> _Ty=char []<br> ]<br> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast<br> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723) : see reference to function template instantiation 'void std::shared_ptr&lt;_Ty>::_Resetp0&lt;_Ux>(_Ux *,std::_Ref_count_base *)' being compiled<br> with<br> [<br> _Ty=char [],<br> _Ux=char<br> ]<br> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723) : see reference to function template instantiation 'void std::shared_ptr&lt;_Ty>::_Resetp0&lt;_Ux>(_Ux *,std::_Ref_count_base *)' being compiled<br> with<br> [<br> _Ty=char [],<br> _Ux=char<br> ]<br> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494) : see reference to function template instantiation 'void std::shared_ptr&lt;_Ty>::_Resetp&lt;_Ux,_Dx>(_Ux *,_Dx)' being compiled<br> with<br> [<br> _Ty=char [],<br> _Ux=char,<br> _Dx=std::default_delete<br> ]<br> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494) : see reference to function template instantiation 'void std::shared_ptr&lt;_Ty>::_Resetp&lt;_Ux,_Dx>(_Ux *,_Dx)' being compiled<br> with<br> [<br> _Ty=char [],<br> _Ux=char,<br> _Dx=std::default_delete<br> ]<br> problem.cpp(9) : see reference to function template instantiation 'std::shared_ptr&lt;_Ty>::shared_ptr>(_Ux *,_Dx)' being compiled<br> with<br> [<br> _Ty=char [],<br> T=char,<br> _Ux=char,<br> _Dx=std::default_delete<br> ]<br> problem.cpp(9) : see reference to function template instantiation 'std::shared_ptr&lt;_Ty>::shared_ptr>(_Ux *,_Dx)' being compiled<br> with<br> [<br> _Ty=char [],<br> T=char,<br> _Ux=char,<br> _Dx=std::default_delete<br> ]<br> problem.cpp(21) : see reference to function template instantiation 'std::shared_ptr&lt;_Ty> make_shared_array(size_t)' being compiled<br> with<br> [<br> _Ty=char []<br> ] </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload