title: Cuda warp illegal address
body: <p>I'm having some trouble with CUDA and passing classes to a kernel. I've some functions which allocate memory for the class on the GPU, pass it, and work fine. There is another one, though, that just won't work. I noticed that it happens only when I'm working with arrays. Here is an example.</p>
  
  <p>File1.hh</p>
  
  <pre><code>#ifndef PROVA1_HH
  #define PROVA1_HH
  
  #include &lt;cstdio&gt;
  
  class cls {
  public:
      int *x, y;
      cls();  
      void kernel();
  };
  
  #endif
  </code></pre>
  
  <p>File1.cu</p>
  
  <pre><code>#include "Prova1.hh"
  
  __global__ void kernel1(cls* c){
      printf("%d\n", c-&gt;y);
      c-&gt;y=2;
      printf("%d\n", c-&gt;y);
      c-&gt;x[0]=0; c-&gt;x[1]=1;
      printf("%d %d\n", c-&gt;x[0], c-&gt;x[1]);
  }
  
  void cls::kernel(){
      cls* dev_c; cudaMalloc(&amp;dev_c, sizeof(cls));
      cudaMemcpy(dev_c, this, sizeof(cls), cudaMemcpyHostToDevice);
      printf("(%d, %d)\n", x[0], x[1]);
      kernel1&lt;&lt;&lt;1, 1&gt;&gt;&gt; (dev_c);
      cudaDeviceSynchronize();
      cudaMemcpy(this, dev_c, sizeof(cls), cudaMemcpyDeviceToHost);
      printf("(%d, %d)\n", x[0], x[1]);
  }
  
  cls::cls(){
      y=3;
      x=(int*) malloc(sizeof(int)*2);
      x[0]=1; x[1]=2;
  }
  </code></pre>
  
  <p>File.cu</p>
  
  <pre><code>#include&lt;cstdio&gt;
  #include "Prova1.hh"
  
  int main(){
      cls c=cls();
      c.kernel();
  
      return 0;
  }
  </code></pre>
  
  <p>I'm compiling with:</p>
  
  <pre><code>nvcc -std=c++11 -arch=sm_35 -rdc=true -c -o File1.o File1.cu
  nvcc -std=c++11 -arch=sm_35 -rdc=true -g -G -o File.out File1.o File.cu
  </code></pre>
  
  <p>When I simpy run it, the output would be:</p>
  
  <pre><code>(1, 2)
  3
  2
  (1, 2)
  </code></pre>
  
  <p>When I debug it, I get:</p>
  
  <pre><code>Starting program: 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
  [New Thread 0x7fb10eb1e0 (LWP 806)]
  (1, 2)
  
  CUDA Exception: Warp Illegal Address
  The exception was triggered at PC 0x84fa10
  
  Thread 1 "File.out" received signal CUDA_EXCEPTION_14, Warp Illegal Address.
  [Switching focus to CUDA kernel 0, grid 1, block (0,0,0), thread (0,0,0), device 0, sm 0, warp 0, lane 0]
  0x000000000084fad0 in kernel1(ciao*)&lt;&lt;&lt;(1,1,1),(1,1,1)&gt;&gt;&gt; ()
  </code></pre>
  
  <p>Do any of you guys know were I'm making mistakes? </p>