I have a c++ vector:
vector<float> floats;
Later, this vector is initialized. I don’t know what the internal contents of the vector container are, but I want to know if it is possible to return a pointer to where the array of floats are. Specifically, I am using CUDA and I need to pass a pointer to an array of floats. I cannot pass it a vector. Is there a way I can do something like
float *dapointer = &vector[0];
or something like this? I want to be able to write to it as well, like
dapointer[some index] = 4;
I realize this violates the point of a vector, but this vector is used for “vector purposes” in many other places in the program, but I need CUDA to be able to access the data as well. I don’t want to change all my other code around to use an array instead of a vector.
The approach should work, but be careful. Upon modifications to the vector, you might be left with dangling pointers. non-
constoperations performed on a vector invalidate iterators, pointers & references to memory it manages. On way to make this safe is marking the vectorconst.