BRabbit27 BRabbit27 - 24 days ago 6
C++ Question

Address of double pointer cast to void**

I was searching how to create an array of pointers and copy it to CUDA memory. I found this post CUDA Double pointer memory copy but the following line in the accepted answer

int ** devicePointersStoredInDeviceMemory;
cudaMalloc( (void**)&devicePointersStoredInDeviceMemory, sizeof(int*)*N);


is bugging me a bit.
What's the meaning of passing the address of a double pointer and cast it to a
void**
?
Wouldn't
&devicePointerStoredInDeviceMemory
be the same as
int***
so why cast it to
void**
instead of
void***
?

It works, but I do not understand why.

Answer

What's the meaning of passing the address of a double pointer and cast it to a void**? Wouldn't &devicePointerStoredInDeviceMemory be the same as int*** so why cast it to void** instead of void***?

The cudaMalloc function takes a void ** as its first parameter, so if we're going to call cudaMalloc, that's the type we need. The purpose of the first parameter is to tell codaMalloc where to store the pointer to the memory we allocated.

You could do this just as well:

void *j;
cudaMalloc(&j, sizeof(int*)*N); // stores result in j
int **devicePointersStoredInDeviceMemory = (int **) j;