Using cudaMallocManaged to resize a vector on the device

Hi

I have inherited some CUDA code which assigns memory on the device using cudaMallocManaged. This is preformed in a function called Resize which resizes the device vector. Initially the vector is NULL and empty, and the vector is incremented one element at a time, and cudaMallocManaged is called to increase the size of the vector an element at a time. However the pointer passed to the call to cudaMallocManaged is always the same (which is a protected member called _data of a class called cudaVector written by whoever wrote the code I have inherited).

My question is: is this method of incrementing the device vector OK? Should I instead create a new pointer, pass that new pointer to cudaMallocManaged, copy the data in the old pointer to the new pointer, delete the old pointer, then assign the protected pointer to the new pointer? I know this is an efficient way to initialise device vectors, but the pointers in the inherited code that cause problems are all assigned by cudaMallocManaged.

What happens to the memory in the current method of resizing described above?

Thanks.

There is no reason to think that the numerical value of a pointer will remain the same in multiple calls to an allocator like cudaMallocManaged, cudaMalloc, cudaHostAlloc, etc.

Just because the pointer passed “is the same” doesn’t mean there is a problem. You can reallocate the same pointer as long as you free it first. If you don’t free it first (in between 2 or more calls to cudaMallocManaged, where the “same pointer” is passed), then you have an opportunity for a memory leak in your application.