Better wrapper for `float*` using `std::vector<float>`
There's this old C API that could use a saner, safer wrapper around it. The API allocates a block of memory by itself and returns the pointer to it (the float* mentioned in the title) and the size. It also takes in float* const and size (though it never reallocates, thankfully)
The usage was quite similar to std::vector and indeed the first thing we did was copying the memory into a vector, freeing the original memory and returning the vector. The option to use std::unique_ptr wasn't considered since we don't get dynamic resizing among other niceties from std::vector.
Is there a way to use the original block of memory and hand it to a vector like class (not necessarily std::vector) without incurring the double memory usage penalty? A custom vector class is a solution, but not that I'd prefer to use.
9 comments · 76% upvoted · other discussions
Consider
gsl::span(GSL) orstd::spanonce C++20 hits.A span is basically a wrapper for a pointer and a size, but won't take ownership of the data.
codingate [OP] 1 points · Jan 2020
Sadly, I'd prefer a owning resource, something about RAII is really nice
biliwald 1 points · Jan 2020
I misunderstood your post. I thought you didn't own the resource.
codingate [OP] 1 points · Jan 2020
Solved!
Almost the correct answer and the one I should have seen at the very beginning.
Use a unique ptr (or not), add a few functions, inherit from span. Sure, it's one extra pointer with unique ptr and some dirt without, but it's a working solution.