C++ 数组的 auto_ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1044785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:32:44  来源:igfitidea点击:

auto_ptr for arrays

c++arraysmemory-management

提问by heavyd

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.

简而言之,我想知道数组是否有类似 auto_ptr 的类型。我知道我可以推出自己的,我只是确保那里还没有什么东西。

I know about vectors as well. however I don't think I can use them. I am using several of the Windows APIs/SDKs such as the Windows Media SDK, Direct Show API which in order to get back some structures to call a function which takes a pointer and a size twice. The first time passing NULL as the pointer to get back the size of the structure that I have to allocated in order to receive the data I am looking for. For example:

我也知道向量。但是我不认为我可以使用它们。我正在使用几个 Windows API/SDK,例如 Windows Media SDK、Direct Show API,它们是为了取回一些结构来调用一个函数,该函数需要一个指针和一个大小两次。第一次传递 NULL 作为指针以取回我必须分配的结构的大小,以便接收我正在寻找的数据。例如:

CComQIPtr<IWMMediaProps> pProps(m_pStreamConfig);
DWORD cbType = 0;
WM_MEDIA_TYPE *pType = NULL;

hr = pProps->GetMediaType(NULL, &cbType);
CHECK_HR(hr);

pType = (WM_MEDIA_TYPE*)new BYTE[cbType];   // Would like to use auto_ptr instread
hr = pProps->GetMediaType(pType, &cbType);
CHECK_HR(hr);

// ... do some stuff

delete[] pType;

Since cbType typically comes back bigger than sizeof(WM_MEDIA_TYPE) due to the fact is has a pointer to another structure in it, I can't just allocate WM_MEDIA_TYPE objects. Is there anything like this out there?

由于 cbType 通常比 sizeof(WM_MEDIA_TYPE) 大,因为它有一个指向另一个结构的指针,我不能只分配 WM_MEDIA_TYPE 对象。外面有这样的东西吗?

回答by Totonga

Use

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

or since C++11

或自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 contiguousthe answer is Yessince C++ 03 standard. There is another threadthat already discussed it.

附加:如果有人问是否保证 Vectors 是连续的,那么答案是Yes,因为 C++ 03 标准。有另一个线程已经讨论过它。



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

如果您的编译器支持C++11,则 unique_ptr 可用于数组。

unique_ptr<BYTE[]> buffer(new BYTE[cbType]);
pType = (WM_MEDIA_TYPE*)buffer.get();

回答by Evan Teran

boost scoped_arrayor you can use boost scoped_ptrwith a custom deleter

boostscoped_array或者您可以将 boostscoped_ptr与自定义删除器一起使用

回答by Jem

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.

当前的标准库中没有任何内容。然而,未来的标准 C++0x 有一个 unique_ptr,它取代了 auto_ptr,并且可以与数组一起使用。

A first implementation can be found here: unique_ptr

第一个实现可以在这里找到: unique_ptr

回答by Fred Larson

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

不在 STL 中。Boost 有一些具有类似想法的智能指针。查看scoped_arrayshared_array