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 8729747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:53:15+00:00 2026-06-13T08:53:15+00:00

Can you make a std::shared_ptr point to an array? For example, std::shared_ptr<int> sp(new int[10]);

  • 0

Can you make a std::shared_ptr point to an array? For example,

std::shared_ptr<int> sp(new int[10]);

If not, then why not? One reason I am already aware of is one can not increment/decrement the std::shared_ptr. Hence it cannot be used like a normal pointer to an array.

  • 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-13T08:53:16+00:00Added an answer on June 13, 2026 at 8:53 am

    With C++17, shared_ptr can be used to manage a dynamically allocated array. The shared_ptr template argument in this case must be T[N] or T[]. So you may write

    shared_ptr<int[]> sp(new int[10]);
    

    From n4659, [util.smartptr.shared.const]

      template<class Y> explicit shared_ptr(Y* p);
    

    Requires: Y shall be a complete type. The expression delete[] p, when T is an array type, or delete p, when T is not an array type, shall have well-defined behavior, and shall not throw exceptions.
    …
    Remarks: When T is an array type, this constructor shall not participate in overload resolution unless the expression delete[] p is well-formed and either T is U[N] and Y(*)[N] is convertible to T*, or T is
    U[] and Y(*)[] is convertible to T*. …

    To support this, the member type element_type is now defined as

    using element_type = remove_extent_t<T>;
    

    Array elements can be access using operator[]

      element_type& operator[](ptrdiff_t i) const;
    

    Requires: get() != 0 && i >= 0. If T is U[N], i < N.
    …
    Remarks: When T is not an array type, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed.


    Prior to C++17, shared_ptr could not be used to manage dynamically allocated arrays. By default, shared_ptr will call delete on the managed object when no more references remain to it. However, when you allocate using new[] you need to call delete[], and not delete, to free the resource.

    In order to correctly use shared_ptr with an array, you must supply a custom deleter.

    template< typename T >
    struct array_deleter
    {
      void operator ()( T const * p)
      { 
        delete[] p; 
      }
    };
    

    Create the shared_ptr as follows:

    std::shared_ptr<int> sp(new int[10], array_deleter<int>());
    

    Now shared_ptr will correctly call delete[] when destroying the managed object.

    The custom deleter above may be replaced by

    • the std::default_delete partial specialization for array types

      std::shared_ptr<int> sp(new int[10], std::default_delete<int[]>());
      
    • a lambda expression

      std::shared_ptr<int> sp(new int[10], [](int *p) { delete[] p; });
      

    Also, unless you actually need share onwership of the managed object, a unique_ptr is better suited for this task, since it has a partial specialization for array types.

    std::unique_ptr<int[]> up(new int[10]); // this will correctly call delete[]
    

    Changes introduced by the C++ Extensions for Library Fundamentals

    Another pre-C++17 alternative to the ones listed above was provided by the Library Fundamentals Technical Specification, which augmented shared_ptr to allow it to work out of the box for the cases when it owns an array of objects. The current draft of the shared_ptr changes slated for this TS can be found in N4082. These changes will be accessible via the std::experimental namespace, and included in the <experimental/memory> header. A few of the relevant changes to support shared_ptr for arrays are:

    — The definition of the member type element_type changes

    typedef T element_type;

     typedef typename remove_extent<T>::type element_type;
    

    — Member operator[] is being added

     element_type& operator[](ptrdiff_t i) const noexcept;
    

    — Unlike the unique_ptr partial specialization for arrays, both shared_ptr<T[]> and shared_ptr<T[N]> will be valid and both will result in delete[] being called on the managed array of objects.

     template<class Y> explicit shared_ptr(Y* p);
    

    Requires: Y shall be a complete type. The expression delete[] p, when T is an array type, or delete p, when T is not an array type, shall be well-formed, shall have well defined behavior, and shall not throw exceptions. When T is U[N], Y(*)[N] shall be convertible to T*; when T is U[], Y(*)[] shall be convertible to T*; otherwise, Y* shall be convertible to T*.

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

Sidebar

Related Questions

std::map<std::string, int> m; // Can I make assumption that m[NoSuchKey] will return 0? std::cout
I can't think of any situation where std::shared_ptr<Object> obj(new Object(foo, 1)); would be preferred
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
I can make my program write a .dat file with an array of Node
I can make a log in for easily, so that's not the problem. What
Can I make all std::string in my application support Unicode with Boost.Locale? After reading
Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things
Consider this minimal example: #include <memory> struct B { typedef std::shared_ptr<B> Ptr; }; struct
Solution To avoid the problem with the std::auto_ptr one can switch to boost::shard_ptr or
How can make a link within a facebox window that redirects it to another

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.