title: Pointers in structs passed to CUDA
body: <p>I've been messing around with this for a while now, but can't seem to get it right. I'm trying to copy objects that contain arrays into CUDA device memory (and back again, but I'll cross that bridge when I come to it):</p>
  
  <pre><code>struct MyData {
    float *data;
    int dataLen;
  }
  
  void copyToGPU() {
    // Create dummy objects to copy
    int N = 10;
    MyData *h_items = new MyData[N];
    for (int i=0; i&lt;N; i++) {
      h_items[i].dataLen = 100;
      h_items[i].data = new float[100];
    }
  
    // Copy objects to GPU
    MyData *d_items;
    int memSize = N * sizeof(MyData);
    cudaMalloc((void**)&amp;d_items, memSize);
    cudaMemCpy(d_items, h_items, memSize, cudaMemcpyHostToDevice);
  
    // Run the kernel
    MyFunc&lt;&lt;&lt;100,100&gt;&gt;&gt;(d_items);
  }
  
  __global__
  static void MyFunc(MyData *data) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    for (int i=0; i&lt;data[idx].dataLen; i++) {
      // Do something with data[idx].data[i]
    }
  }
  </code></pre>
  
  <p>When I call MyFunc(d_items), I can access data[idx].dataLen just fine. However, data[idx].data has not been copied yet.</p>
  
  <p>I can't use d_items.data in copyToGPU as a destination for cudaMalloc/cudaMemCpy operations since the host code cannot dereference a device pointer.</p>
  
  <p>What to do?</p>