pointer to pointer void** devPtr Pointer help!


I have successfully run many CUDA programs. In almost every program I have used cudamalloc(); function. But I am unable to understand exactly why a pointer to a pointer variable i.e.void** devPtr is used in the function argument.

The definition of the function from the reference guide is as below:

1.6.1 cudaMalloc
cudaMalloc - allocate memory on the GPU
cudaError_t cudaMalloc( void** devPtr, size_t count )
Allocates count bytes of linear memory on the device and returns in *devPtr a pointer to the allocatedmemory. The allocated memory is suitably aligned for any kind of variable. The memory is not cleared.cudaMalloc() returns cudaErrorMemoryAllocation in case of failure.

For any pointers to this pointer question will be highly appreciated. :-)


cudaMalloc allocates a memory somewhere at position P. Therefore P is an address - type (void*).
That value P must be returned, however - unlike malloc() it is not returned by function, but passed back by parameter.

When a function parameter can be changed (so that results can be seen afterwards), you do not pass the value of a variable but a pointer to that variable.
A well known example is a scanf function: you do something like scanf("%d",&x), where &x is an address of variable you want to change.

So, in case of cudaMalloc, you want to change the value of your device pointer, so that it will be equal to P - you pass an address of that variable – address of (void*) variable – hence void**.

Can’t it be implemented like this?


#include <stdio.h>


void assignPointer(T* & p) {

p = (T*)0xFF;


int main(){

float * tmp = 0;







It can, but C API does not use reference parameters nor templates as there are no such constructs in C.

Right, the C-thingy slipped my mind :)


Why can’t it be a reference to a pointer instead?

Check the other posts in this thread.