title: Use of cudamalloc(). Why the double pointer?
body: <p>I am currently going through the tutorial examples on <a href="http://code.google.com/p/stanford-cs193g-sp2010/">http://code.google.com/p/stanford-cs193g-sp2010/</a> to learn CUDA. The code which demostrates <code>__global__</code> functions is given below. It simply creates two arrays, one on the CPU and one on the GPU, populates the GPU array with the number 7 and copies the GPU array data into the CPU array. </p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  __global__ void kernel(int *array)
  {
    int index = blockIdx.x * blockDim.x + threadIdx.x;
  
    array[index] = 7;
  }
  
  int main(void)
  {
    int num_elements = 256;
  
    int num_bytes = num_elements * sizeof(int);
  
    // pointers to host &amp; device arrays
    int *device_array = 0;
    int *host_array = 0;
  
    // malloc a host array
    host_array = (int*)malloc(num_bytes);
  
    // cudaMalloc a device array
    cudaMalloc((void**)&amp;device_array, num_bytes);
  
    int block_size = 128;
    int grid_size = num_elements / block_size;
  
    kernel&lt;&lt;&lt;grid_size,block_size&gt;&gt;&gt;(device_array);
  
    // download and inspect the result on the host:
    cudaMemcpy(host_array, device_array, num_bytes, cudaMemcpyDeviceToHost);
  
    // print out the result element by element
    for(int i=0; i &lt; num_elements; ++i)
    {
      printf("%d ", host_array[i]);
    }
  
    // deallocate memory
    free(host_array);
    cudaFree(device_array);
  } 
  </code></pre>
  
  <p>My question is why have they worded the <code>cudaMalloc((void**)&amp;device_array, num_bytes);</code> statement with a double pointer? Even <a href="http://www.clear.rice.edu/comp422/resources/cuda/html/group__CUDART__MEMORY_gc63ffd93e344b939d6399199d8b12fef.html">here</a> definition of cudamalloc() on says the first argument is a double pointer.  </p>
  
  <p>Why not simply return a pointer to the beginning of the allocated memory on the GPU, just like the <code>malloc</code> function does on the CPU?</p>