Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Different syntax for shared arrays between unique_ptr and shared_ptr
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
SG  
View profile  
 More options Apr 15 2012, 3:37 pm
Newsgroups: comp.lang.c++.moderated
From: SG <s.gesem...@gmail.com>
Date: Sun, 15 Apr 2012 12:37:29 -0700 (PDT)
Local: Sun, Apr 15 2012 3:37 pm
Subject: Re: Different syntax for shared arrays between unique_ptr and shared_ptr
On 15 Apr., 08:29, Edward Diener wrote:

> As I understand it specifying a non-shared array with unique_ptr is
> done using the syntax:

>   std::unique_ptr<T[]> up(new T[n]);

> The unique_ptr has a partial specialization to handle this.

> The syntax for handling shared arrays with shared_ptr is different:

>   std::shared_ptr<T> sp(new T[n],std::default_delete<T[]>());

> Why is the syntax for working with arrays in unique_ptr and shared_ptr
> different ?

The short and easy answer: because nobody proposed to add a T[]
specialization to shared_ptr (not to my knowledge).

I just want to point out that shared_ptr needs more book keeping
(reference counter). So, you'd have two allocated memory blocks, the
ref counter and the array itself. This memory layout is very similar
to the layout you get by writing

   auto sv = make_shared<vector<T>>(n);

on any decent implementation that would allocate the sizeof(vector<T>)
bytes together with the ref counting book keeping bits. You're just
getting an an additional level of indirection if you access vector
elements via sv. Though, You can avoid this using the vector's
iterators for a loop. So, what I'm saying is that I see little reason
to prefer sharedptr<T> with an array deleter over a
shared_ptr<vector<T>>.

Cheers!
SG

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.