Reprint please indicate the source :

http://www.cnblogs.com/darkknightzh/p/5462363.html

Refer to the website :

http://stackoverflow.com/questions/13061979/shared-ptr-to-an-array-should-it-be-used

By default ,std::shared_ptr Would call delete To clear the memory . When using new[] When allocating memory , Need to call delete[] To free up memory , Otherwise, there will be a memory leak .

You can customize the function to free memory with the following code :

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

Declare with the following code std::shared_ptr The pointer :

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

here ,shared_ptr Can be called correctly delete[].

stay C++11 in , have access to  std::default_delete Instead of what I wrote above array_deleter:

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

You can also use lambda Expressions come from defining delete functions

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

actually , Unless you need to share goals , otherwise unique_ptr It's better to use arrays :

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

ps, The above code can allocate space correctly , But none of the values in the space are initialized . If necessary, the default initialization is 0, You can use the following code :

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

ps2, Use vector when , Can pass fill Function to vector Set all elements in to default values .

vector<unsigned char> data(dataLen);
std::fill(data.begin(), data.end(), );

( translate + primary )std::shared_ptr And std::unique_ptr More articles on using arrays

  1. std::shared_ptr and std::weak_ptr And the circular reference problem of reference counting

    stay std::shared_ptr Before being introduced ,C++ The only intelligent pointer used to manage resources in the standard library is std::auto_ptr Just one .std::auto_ptr It's very limited , Because it has ownership transfer of managed resources ...

  2. Item 20: Use std::weak_ptr Replacing classes that cause pointer dangling std::shared_ptr The pointer

    This article is translated from modern effective C++, Due to limited level , Therefore, there is no guarantee that the translation is completely correct , Welcome to point out the mistakes . thank you ! Blog has moved here Paradoxically , It's easy for us to create one and std::shared_ptr ...

  3. Item 21: Compared with direct use new priority of use std::make_unique and std::make_shared

    This article is translated from modern effective C++, Due to limited level , Therefore, there is no guarantee that the translation is completely correct , Welcome to point out the mistakes . thank you ! Blog has moved here Let's start with std::make_unique and std::make_s ...

  4. std::shared_ptr&lt;void&gt; How it works

    Foreplay Let's start with two questions If delete A pointer , But its real type is not the same as the pointer type, what happens ? Who called the destructor ? What's interesting about the following code ? // delete_diff_type.cpp ...

  5. std::shared_ptr( Two )

    Defined in header <memory>       template< class T > class shared_ptr;   (since C++11)   ...

  6. std::shared_ptr

    stay std::shared_ptr Before being introduced ,C++ The only intelligent pointer used to manage resources in the standard library is std::auto_ptr Just one .std::auto_ptr It's very limited , Because it has ownership transfer of managed resources ...

  7. Skillfully use std::shared_ptr Global objects free singleton memory

    Skillfully use std::shared_ptr Singletons are relatively widely used , But it needs to call its destructor before the program exits to release the data , The normal practice is to main Release at the end of the function , But this is relatively cumbersome , So there's the use of global variables ...

  8. tolua transformation std::shared_ptr

    tolua transformation std::shared_ptr since c++11 in the future std::shared_ptr It's almost more than just something to use , The following applications are often encountered std::shared_ptr<Tst_ShareTe ...

  9. std::shared_ptr And ordinary pointer conversion

    See examples for mutual transformation struct test { int num; string name; }; test* pTest = new test(); std::shared_ptr<test> ...

Random recommendation

  1. java A simple example of reflection in

    package club.reflection.entity.User; /** * Entity class * */ public class User { public String name; private ...

  2. machine learning : Anomaly detection algorithm Seasonal Hybrid ESD And R Language implementation

    Twritters Anomaly detection algorithm of (Anomaly Detection) Well done ,Seasonal Hybrid ESD The algorithm is used first STL Break down the sequence , Examine the residual term . Suppose that this term is normally distributed , And then you can use it Ge ...

  3. 【 turn 】UIKit Performance tuning practical explanation

    writing /bestswifter( Jane book author ) Link to the original text :http://www.jianshu.com/p/619cf14640f3 The copyright belongs to the author , Please contact the author for permission to reprint , And mark “ Jane book author ”. In the use of UIKi ...

  4. AUC Summary of calculation methods

    One .roc curve 1.roc curve : Receiver operating characteristics (receiveroperating characteristic),roc Each point on the curve reflects the sensitivity to the same signal stimulation . The horizontal axis : Negative positive rate (false pos ...

  5. AMS The applicable scenarios of

    AMS It is suitable for all kinds of network audio and video applications , It can be used independently as a live on demand platform , It can also be embedded in various application platforms of users , Provide audio and video core support for customers , Different from other products that provide cloud services for customers to rent ,AMS It's a set of servers installed inside the enterprise ...

  6. MyDAL - Quick to use

    Indexes : indexes One . install stay VS So let's do that package command : PM> Install-Package MyDAL Two .API- Quick to use 1. Namespace , just : using MyDAL; ...

  7. Why write ahead of time Spec file

    Joel on Software( Chinese name <Joel Software random record >) It's an old book , But the suggestions and discussions in it , It's really lasting . especially ,Joel It's an interesting . Tough guy : Former Microsoft Excel Staff of .St ...

  8. Androidn Notification Use , The solution can't be found setLatestEventInfo Method

    from :http://blog.csdn.net/songyachao/article/details/51245370#comments Use today 4.0.3 Use Notification notifi ...

  9. SDN Learning notes

    SDN What is? SDN SDN It's a framework and an idea , The core demand is to control the network through software , Realize the automatic deployment of business , For the convenience of software to control the network , I hope the control surface and the forwarding surface are separated . for example , Inside the traditional switch , The switch is responsible for the specific network traffic to ...

  10. ROS Registration level LEVEL0-6, The original use GRE The channel is free of money

    GRE There is no limit to the number of channels . If only one branch company PPTP,L2TP, wait , I don't need money .  run OSPF No way. , Money has to be given .