I need a smart buffer that uses malloc
and free
such as for example:
shared_ptr<byte[]> buffer((byte*) malloc(123), free)
unique_ptr<byte[], decltype(&free)> buffer((byte*) malloc(123), free);
However I find it tedious and error-prone to separately keep track of the size of the buffer.
The purpose of this smart array pointer is to store a buffer of binary data inside a class, which can be passed around to other functions, and which could later be release()
ed and passed to a C library which will handle the memory and later call free()
. I need to accomplish this without copying the buffer.
I could wrap a unique_ptr
and a size
variable in a struct but that seems quite inelegant. Getting the pointer would then be static_vector.arr.get()
, accessing an element of the array would be static_vector.arr[i]
which I don't like at all. I guess I could define the []
operator and the get()
function. I also want to be able to call release()
so I'd end up making a decent sized wrapper for unique_ptr when I literally just want a unique_ptr with one little field to keep track of the size (why isn't it there in the first place?)
Is it possible to create a class which inherits std::unique_ptr<byte[]>
and adds a size field?