views:

1110

answers:

4

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.

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:

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?

+4  A: 

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

Evan Teran
+2  A: 

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

Fred Larson
+13  A: 

Use

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

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.

Totonga
Is there any use case in local programming that can not be solved by using a std::vector instead of new []?
Totonga
Yup. Vectors, not arrays.
Assaf Lavie
Are vectors guaranteed to be contiguous blocks of memory?
heavyd
@Heavyd Yes - the c++ standard guarantees this.
anon
I'm all for using std::vector, but if the array isn't intended to be resizable (and in this case, isn't really an array at all) std::vector might be a little overkill.
Evan Teran
@Evan - why? At runtime it boils down to just a pointer to a hunk of memory.
Daniel Earwicker
@Evan I disagree. If you don't use a std::vector you are going to have to use a smart pointer or write all the delete code yourself, fielding exception problems etc. In either case, std::vector is no more complex, and may well be simpler.
anon
Because std::vector is a template the compiler will either way only create the code that is really needed. So at the end it will end up in the same code that you need for your new/delete pair.
Totonga
@Earwicker a pointer, memory, plus current size and potential size.
Thomas L Holaday
+2  A: 

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

Jem
you should make it clear that for unique_ptr to work, you'll need to supply a custom deleter.
Evan Teran
Not necessarily, see section "Safe Support For Arrays"
Jem
ahh, I stand corrected. +1 for you ;).
Evan Teran