title: CUDA is it safe to copy a single element from device memory by array offset?
body: <pre><code>int main()
  {
      int n = 1000;
      float *d_a;
      cudaMalloc(&amp;d_a, n * sizeof(float));
  
      // do something that gives d_a value;
      int index = rand() % n; // copy index
      float res;
      cudaMemcpy(&amp;res, &amp;d_a[index], sizeof(float), cudaMemcpyDeviceToHost);
      cudaMemcpy(&amp;res, d_a + index, sizeof(float), cudaMemcpyDeviceToHost);
      // Are the above 2 the same and legal ?
      cout &lt;&lt; res &lt;&lt; &quot;\n&quot;;
  }
  return 0;
  </code></pre>
  <p>So I have the following problem. I need to copy exactly 1 element (random index) from the device array. I don't want to copy the entire array because it'd be wasteful.</p>
  <p>So, is the above methods safe and conform to CUDA programming guide?</p>