If I have a NSData* from one API and need to turn that into an std::vector for another API, is there any way to do this safely without copying the bytes in the NSData (assume a very large NSData*)?
No quick solution that comes to mind, no.
If you create the NSData object (rather than it being returned by another API), you might consider subclassing NSData
and/or NSMutableData
when you really need to avoid the copy -- then you could access the storage (std::vector) directly.
You may also be able to sneak around it in some cases by creating the data with 'your' allocation:
NSData * data([[NSData alloc]
initWithBytesNoCopy:vector.data()
length:vector.size()
freeWhenDone:false]);
of course, you need to make sure the vector is not resized (or its backing store reallocated) before the NSData
object is deallocated.
Sometimes you also need to consider changing the parameter type so that it is not a std::vector
. A little container which has a NSData
member and a vector-like interface, iterator, or begin+end may be adequate while accommodating other collections types.
I don't know enough about stl or cpp, but if you can construct a vector from a void* buffer then you should be able to with:
void * buffer = [data bytes];
size_t len = [data length];
keep in mind that the data owns the buffer so you may not free it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With