vector vs unique_ptr to c style function

265 Views Asked by At

When I am interacting with a c-style function which style should I use.

I do plan on storing the data in a vector after it comes back from the c function. What are advantageous of one over the other?

{
  auto test = std::make_unique<double[]>(10);
  fooCstyle(test);
}

or

{
  auto test = std::vector<double>;
  test.reserve(10);
  fooCstyle(test);
}
1

There are 1 best solutions below

5
On BEST ANSWER

In this case it makes no difference, it depends on what you want to do with that data later in cpp-styled code.

However your examples are wrong, it should look like this:

std::vector<char> buffer(10);
cstyle(buffer.data());

or

std::unique_ptr<char[]> test { new char[10] };
cstyle(test.get());