Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:15:17+00:00 2026-06-19T00:15:17+00:00

I wanted to replace some raw pointers in my class with a std::shared_ptr so

  • 0

I wanted to replace some raw pointers in my class with a std::shared_ptr 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. g. default_delete<T[]>.

But I get a big error list as soon as I try to assign a new value to that field, even on construction.

Here’s a minimal code sample:

#include <memory>
#include <cstddef>

using namespace std;

template<typename T> shared_ptr<T[]> make_shared_array(size_t size)
{
  return shared_ptr<T[]>(new T[size], default_delete<T[]>());
}

struct Foo
{
  shared_ptr<char[]> field;
};

int main()
{
  Foo a;
  // This line produces the error.
  a.field = make_shared_array<char>(256);

  return 0;
}

NB: Yes, I know that I could/should vector 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 s. That’s quite a lot.


Update: 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:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(754) : error C2664: ‘std::_Ptr_base<_Ty>::_Reset0’ : cannot convert parameter 1 from ‘char ‘ to ‘char ()[]’
with
[
_Ty=char []
]
Types pointed to are 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 to function template instantiation ‘void std::shared_ptr<_Ty>::_Resetp0<_Ux>(_Ux *,std::_Ref_count_base *)’ being compiled
with
[
_Ty=char [],
_Ux=char
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(723) : see reference to function template instantiation ‘void std::shared_ptr<_Ty>::_Resetp0<_Ux>(_Ux *,std::_Ref_count_base *)’ being compiled
with
[
_Ty=char [],
_Ux=char
]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\memory(494) : see reference to function template instantiation ‘void std::shared_ptr<_Ty>::_Resetp<_Ux,_Dx>(_Ux *,_Dx)’ being 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 to function template instantiation ‘void std::shared_ptr<_Ty>::_Resetp<_Ux,_Dx>(_Ux *,_Dx)’ being compiled
with
[
_Ty=char [],
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(9) : see reference to function template instantiation ‘std::shared_ptr<_Ty>::shared_ptr>(_Ux *,_Dx)’ being compiled
with
[
_Ty=char [],
T=char,
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(9) : see reference to function template instantiation ‘std::shared_ptr<_Ty>::shared_ptr>(_Ux *,_Dx)’ being compiled
with
[
_Ty=char [],
T=char,
_Ux=char,
_Dx=std::default_delete
]
problem.cpp(21) : see reference to function template instantiation ‘std::shared_ptr<_Ty> make_shared_array(size_t)’ being compiled
with
[
_Ty=char []
]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:15:18+00:00Added an answer on June 19, 2026 at 12:15 am

    The solution you suggest is possible, but you will lose the size of the array:

    #include <memory>
    #include <cstddef>
    
    using namespace std;
    
    template<typename T> shared_ptr<T> make_shared_array(size_t size)
    {
       return 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);
    
     return 0;
    }
    

    What I have done here is to let the array decay into a pointer. As long as the deleter is an array deleter it should behave correctly.

    To prevent this loss of size, and if you cannot use boost::shared_array as suggested, I would suggest to encapsulate this information in your own shared_array class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wrapper procedure(proc_main) that calls some procedures within. create or replace Procedure
I have some code that I had to write to replace a function that
1) I have the binary file that I wanted to replace with a newer
I wanted to replace not just values stored in columns with parameters, but table
I have the following code in HTML and wanted to replace them using javascript
I have never used dll's before(absolutely no experience) and I wanted to replace a
I wanted to copy files from a remote server, but it seems that the
We have a database that has a bunch of records with some bad data
I wanted to ask, I have an array and I want to eliminate some
I'm playing with some design patterns, and wanted to create an example using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.