title: Basic CUDA pointer/array memory allocation and use
body: <p>I started CUDA last week as I have to convert an existing c++ programme to cuda for my research. </p>
  
  <p>This is a basic example from the CUDA by Example book, which I reccommend to anyone who wants to learn CUDA!</p>
  
  <p>Can someone explain how you can assign GPU memory with 'dev_c' which is an empty pointer?</p>
  
  <pre><code>HANDLE_ERROR( cudaMalloc( (void**)&amp;dev_c, N * sizeof(int) ) );
  </code></pre>
  
  <p>Then, not pass any 'dev_c' values when calling the function 'add' but treat *c as an array in the global function and write to it from within the function? Why is this possible when its not defined as an array anywhere?</p>
  
  <pre><code>add&lt;&lt;&lt;N,1&gt;&gt;&gt;( dev_a, dev_b, dev_c );
  </code></pre>
  
  <p>Finally, where exactly do the terms c[0], c[1] etc. get saved when performing the following addition?</p>
  
  <pre><code>c[tid] = a[tid] + b[tid];
  </code></pre>
  
  <p>I hope I am explaining myself well but feel free to ask any follow-up questions. New to C as well as CUDA so be nice :D</p>
  
  <p>Entire code below:</p>
  
  <pre><code>#include "book.h"
  
  #define N   1000
  
  __global__ void add( int *a, int *b, int *c ) {
      int tid = blockIdx.x;    // this thread handles the data at its thread id
      if (tid &lt; N)
          c[tid] = a[tid] + b[tid];
  }
  
  int main( void ) {
      int a[N], b[N], c[N];
      int *dev_a, *dev_b, *dev_c;
  
      // allocate the memory on the GPU
      HANDLE_ERROR( cudaMalloc( (void**)&amp;dev_a, N * sizeof(int) ) );
      HANDLE_ERROR( cudaMalloc( (void**)&amp;dev_b, N * sizeof(int) ) );
      HANDLE_ERROR( cudaMalloc( (void**)&amp;dev_c, N * sizeof(int) ) );
  
      // fill the arrays 'a' and 'b' on the CPU
      for (int i=0; i&lt;N; i++) {
          a[i] = -i;
          b[i] = i * i;
      }
  
      // copy the arrays 'a' and 'b' to the GPU
      HANDLE_ERROR( cudaMemcpy( dev_a, a, N * sizeof(int),
                                  cudaMemcpyHostToDevice ) );
      HANDLE_ERROR( cudaMemcpy( dev_b, b, N * sizeof(int),
                                  cudaMemcpyHostToDevice ) );
  
      add&lt;&lt;&lt;N,1&gt;&gt;&gt;( dev_a, dev_b, dev_c );
  
      // copy the array 'c' back from the GPU to the CPU
      HANDLE_ERROR( cudaMemcpy( c, dev_c, N * sizeof(int),
                                  cudaMemcpyDeviceToHost ) );
  
      // display the results
      for (int i=0; i&lt;N; i++) {
          printf( "%d + %d = %d\n", a[i], b[i], c[i] );
      }
  
      // free the memory allocated on the GPU
      HANDLE_ERROR( cudaFree( dev_a ) );
      HANDLE_ERROR( cudaFree( dev_b ) );
      HANDLE_ERROR( cudaFree( dev_c ) );
  
      return 0;
  }
  </code></pre>
  
  <p>Thank you!</p>