32

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?

jww
  • 97,681
  • 90
  • 411
  • 885
heavyd
  • 17,303
  • 5
  • 56
  • 74

4 Answers4

42

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();
Community
  • 1
  • 1
Totonga
  • 4,236
  • 2
  • 25
  • 31
  • Are vectors guaranteed to be contiguous blocks of memory? – heavyd Jun 25 '09 at 16:20
  • 5
    @Heavyd Yes - the c++ standard guarantees this. –  Jun 25 '09 at 16:22
  • 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 Jun 25 '09 at 16:29
  • @Evan - why? At runtime it boils down to just a pointer to a hunk of memory. – Daniel Earwicker Jun 25 '09 at 16:39
  • 4
    @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. –  Jun 25 '09 at 16:41
  • 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 Jun 26 '09 at 07:21
  • @Earwicker a pointer, memory, plus current size and potential size. – Thomas L Holaday Jun 26 '09 at 11:01
  • I went with the unique_ptr solution since vector was overkill for my arrays (They never need to be resized or anything like that) – Nico Jun 14 '13 at 21:40
  • 1
    Can you fix the example of using std::vector<>::data(). data() already returns a pointer, so no need for '&'. – Markus Mayer Apr 17 '15 at 14:11
  • Would there be a reason to use `buffer.data();` over `&buffer[0];` or is it just being mentioned because it is a possible variation after C++11? – Puddler Dec 27 '18 at 03:26
  • `buffer.data();`is defined to not throw if container is empty. It will return nullptr instead. `&buffer[0]`might or will throw. So using `data()` avoid the need of extra code for zero size containers. – Totonga Mar 11 '19 at 09:15
10

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

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
9

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
  • 2,255
  • 18
  • 25
3

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

Fred Larson
  • 60,987
  • 18
  • 112
  • 174