In C++ programming, I often need to acquire the address of a pointer and convert it to void **
. For example, typically when using CUDA:
int main() {
float *data;
size_t size = sizeof(float) * 1024;
CUDA_CHECK(cudaMalloc((void **)&data, size)); // question about this line
...
CUDA_CHECK(cudaFree(static_cast<void *>(data)));
return 0;
}
My problem is that the cast of a float **
to void **
using C-style type cast irks me, as I don't want to use that in C++ programming.
So far, I've been using reinterpret_cast<void **>(&data)
instead, but I'm not happy with it. For some reason I don't think this is the proper way to do it.
I tried doing &static_cast<void *>(data)
once very stupidly, as this obviously fails for trying to get the address from a rvalue.
I've done some search on Google and Stack Overflow, but I'm having a hard time finding good keywords for the query.
I've also been learning the concept of rvalue reference, but I don't think it was designed for this problem.
So my question is, what is the proper C++ way to cast from float **
to void **
in this context? Also, is there a way to static_cast
to void *
and still get the address from the returned value, like rvalue reference? (PS: I still don't quite understand rvalue reference, so forgive me if this is a rookie mistake)
Aucun commentaire:
Enregistrer un commentaire