Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c++ - TR1 Shared Arrays

I've had a hard time finding references in the TR1 documentation concerning shared arrays. The Boost documentation is fairly clear that there is a significant difference between the C++ "new" and "new[]" expressions. The shared_ptr template is meant to correctly hold a pointer to a dynamically allocated objected created using "new". The shared_array template is meant to correctly hold a pointer to a dynamically allocated array using "new[]".

I'm in the process of updating some code to use the TR1 shared_ptr template and associated functions, but I've found no mention of shared_array. Does the TR1 shared_ptr implementation differentiate between "new" and "new[]", and destroy those pointers correctly? As far as I can tell from looking at the TR1 spec, it appears it does not. If this is the case, should I still be using the boost shared_array template for "new[]" style allocations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That is correct, there is no shared_array in TR1.

You can, however, provide your own deleter object to perform "delete []" if you wish using this constructor:

template<class Other, class D>
   shared_ptr(Other* ptr, D dtor);

For example:

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

shared_ptr<int> sp(new int[100], my_array_deleter<int>());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...