home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,144 of 33,346   
   Edward Diener to All   
   Re: Different syntax for shared arrays b   
   15 Apr 12 18:20:16   
   
   d41d2999   
   From: eldiener@tropicsoft.invalid   
      
   On 4/15/2012 3:37 PM, SG wrote:   
   > 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  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  sp(new T[n],std::default_delete());   
   >>   
   >> 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>(n);   
   >   
   > on any decent implementation that would allocate the sizeof(vector)   
   > 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  with an array deleter over a   
   > shared_ptr>.   
      
   I agree that using std::vector instead of C-style arrays is my preferred   
   way of working. Nonetheless if C-style arrays were supported with both   
   std::unique_ptr and std::shared_ptr it would seem beneficial to use the   
   same syntax for both. Evidently that never happened.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca