How to get the address of the std::vector buffer start most elegantly?

后端 未结 11 2212
-上瘾入骨i
-上瘾入骨i 2020-12-03 18:12

I want to use std::vector for dynamically allocating memory. The scenario is:

int neededLength = computeLength(); // some logic here

// this will allocate t         


        
11条回答
  •  心在旅途
    2020-12-03 18:37

    If you're using std::vector just for its RAII properties (so that it will free the memory for you), and you don't actually need it to resize or anything, you might be better off using a Boost scoped_array

    boost::scoped_array buffer( new TCHAR[neededLength] );
    callFunction( buffer.get(), neededLength );
    

    scoped_array will call delete[] on the array when it goes out of scope.

提交回复
热议问题