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
NAME
cudaMalloc - allocate memory on the GPU
SYNOPSIS
cudaError_t cudaMalloc( void** devPtr, size_t count )
DESCRIPTION
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**.