Is there a way to transfer ownership of the data contained in std :: vector (denoted by, say, T * data) to another construction, preventing the "data" from becoming a sagging pointer after the vector goes beyond the bounds?
EDIT: I DO NOT WANT TO COPY DATA (which would be a simple but ineffective solution).
In particular, I would like to have something like:
template<typename T>
T* transfer_ownership(vector<T>&v){
T*data=&v[0];
v.clear();
...
}
int main(){
T*data=NULL;
{
vector<double>v;
...
data=transfer_ownership<double>(v);
}
...
}
The only thing that comes to mind to imitate this is:
{
vector<double>*v=new vector<double>();
data=(*v)[0];
}
and then the data will either be freed or (in my case) to be used as mxSetData (mxArrayA, doubleledata). However, this leads to a small memory leak (data structure for processing v capacity, size, etc., but not the data itself, of course).
Is a leak possible?