auto_ptr for arrays

后端 未结 4 1189
[愿得一人]
[愿得一人] 2020-12-09 15:03

In short, I am wondering if there is an auto_ptr like type for arrays. I know I could roll my own, I\'m just making sure that there isn\'t already something out there.

相关标签:
4条回答
  • 2020-12-09 15:33

    Use

    std::vector<BYTE> buffer(cbType);
    pType = (WM_MEDIA_TYPE*)&buffer[0];
    

    or since C++11

    std::vector<BYTE> buffer(cbType);
    pType = (WM_MEDIA_TYPE*)buffer.data();
    

    instead.


    Additional: If someone is asking if the Vectors are guaranteed to be contiguous the answer is Yes since C++ 03 standard. There is another thread that already discussed it.


    If C++11 is supported by your compiler, unique_ptr can be used for arrays.

    unique_ptr<BYTE[]> buffer(new BYTE[cbType]);
    pType = (WM_MEDIA_TYPE*)buffer.get();
    
    0 讨论(0)
  • 2020-12-09 15:33

    There is nothing for this in the current std library. However, the future standard C++0x has an unique_ptr, which comes in replacement of auto_ptr, and which works with arrays.

    A first implementation can be found here: unique_ptr

    0 讨论(0)
  • 2020-12-09 15:44

    boost scoped_array or you can use boost scoped_ptr with a custom deleter

    0 讨论(0)
  • 2020-12-09 15:57

    Not in STL. Boost has some smart pointers with a similar idea. Check out scoped_array and shared_array

    0 讨论(0)
提交回复
热议问题