title: How to pass a pointer in a class to a kernel in opencl c++ and get pointer data
body: <p>The platform i am using is AMD radeon 200 series (sapphire 250 (GPU- Oland)
  I am creating a class on host having a pointer as data member, and same class on kernel side too.
  now if in kernel side, pointer is present in the class, build error( -11) arises. if we remove pointer on kernel side, code builds up.
  there, the pointer value is copied to a global pointer and  dereferenced value comes to be zero.</p>
  
  <p>My main objective is to access a pointer in a class on device side.</p>
  
  <p>AMD SDK-3.0
  opencl c++ version 1.2</p>
  
  <p>any help would be appreciated.</p>
  
  <pre><code>  class A
    {
     public:
    int* ptr;
    };
  
  int main()
  {
   const int LIST_SIZE = 1;
  
   int abc=20;
   A *obj=new A;
   obj-&gt;ptr= &amp;abc;
  printf("\nx=%d\n",*(obj-&gt;ptr));
  
   int *A = new int[LIST_SIZE]; 
  int *B = new int[LIST_SIZE];
  cl_int z;
  for(int i = 0; i &lt; LIST_SIZE; i++) {
      A[i] = i;
      B[i] = LIST_SIZE - i;
  }
  
  
      // Get available platforms
      vector&lt;Platform&gt; platforms;
      Platform::get(&amp;platforms);
  
      // Select the default platform and create a context using this platform and the GPU
      cl_context_properties cps[3] = { 
          CL_CONTEXT_PLATFORM, 
          (cl_context_properties)(platforms[0])(), 
          0 
      };
      Context context( CL_DEVICE_TYPE_GPU, cps,NULL,NULL,&amp;z);
  
      // Get a list of devices on this platform
      vector&lt;Device&gt; devices = context.getInfo&lt;CL_CONTEXT_DEVICES&gt;();
  
      // Create a command queue and use the first device
      CommandQueue queue = CommandQueue(context, devices[0],NULL,&amp;z);
  
       //Read source file
      std::ifstream sourceFile("kernel.cl");
      std::string sourceCode(
          std::istreambuf_iterator&lt;char&gt;(sourceFile),
          (std::istreambuf_iterator&lt;char&gt;()));
      Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length()+1));
  
    // Make program of the source code in the context
      Program program = Program(context, source,&amp;z);
  
    // Build program for these specific devices
      z=program.build(devices,"-x clc++",NULL,NULL);
      if(z!=CL_SUCCESS){
              cout&lt;&lt;"build"&lt;&lt;endl;return 1;}
     // Make kernel
       Kernel kernel(program, "vector_add",&amp;z);
  
      // Create memory buffers
      Buffer bufferA = Buffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int),NULL,&amp;z);
      Buffer bufferB = Buffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int),NULL,&amp;z);
      Buffer bufferC = Buffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int),NULL,&amp;z);
      Buffer bufferD = Buffer(context, CL_MEM_READ_WRITE, sizeof(obj),NULL,&amp;z);
  
  
      // Copy lists A and B to the memory buffers
      z= queue.enqueueWriteBuffer(bufferA, CL_TRUE, 0, LIST_SIZE * sizeof(int), A,NULL,NULL);
      if(z!=CL_SUCCESS){
              cout&lt;&lt;"enqueue buff A"&lt;&lt;endl;return 1;}
  
      z=queue.enqueueWriteBuffer(bufferB, CL_TRUE, 0, LIST_SIZE * sizeof(int), B,NULL,NULL);
      if(z!=CL_SUCCESS){
              cout&lt;&lt;"enqueue buffB"&lt;&lt;endl;return 1;}
  
      z=queue.enqueueWriteBuffer(bufferD, CL_TRUE, 0, sizeof(obj), obj,NULL,NULL);
      if(z!=CL_SUCCESS){
              cout&lt;&lt;"enqueue buffB"&lt;&lt;endl;return 1;}
  
      // Set arguments to kernel
      z= kernel.setArg(0, bufferA);
       if(z!=CL_SUCCESS){
              cout&lt;&lt;"kerarg A"&lt;&lt;endl;return 1;}
      z= kernel.setArg(1, bufferB);
       if(z!=CL_SUCCESS){
              cout&lt;&lt;"kerarg buff B"&lt;&lt;endl;return 1;}
      z= kernel.setArg(2, bufferC);
       if(z!=CL_SUCCESS){
           cout&lt;&lt;"kerarg C"&lt;&lt;endl;return 1;}
  
          z= kernel.setArg(3, bufferD);
       if(z!=CL_SUCCESS){
           cout&lt;&lt;"kerarg C"&lt;&lt;endl;return 1;}
  
  
  
  
      // Run the kernel on specific ND range
      NDRange global(LIST_SIZE);
      NDRange local(1);
      queue.enqueueNDRangeKernel(kernel, NullRange, global, local,NULL,NULL);
  
      // Read buffer C into a local list
      int *C = new int[LIST_SIZE];
      queue.enqueueReadBuffer(bufferC, CL_TRUE, 0, LIST_SIZE * sizeof(int), C,NULL,NULL);
       queue.enqueueReadBuffer(bufferD, CL_TRUE, 0, sizeof(obj), obj,NULL,NULL);
  
      for(int i = 0; i &lt; LIST_SIZE; i ++)
           std::cout &lt;&lt; A[i] &lt;&lt; " + " &lt;&lt; B[i] &lt;&lt; " = " &lt;&lt; C[i] &lt;&lt; std::endl; 
  
  
      printf("\nx=%d\n",*(obj-&gt;ptr));
  
  return 0;
  }
  </code></pre>
  
  <p>and kernel code is</p>
  
  <pre><code> class A
  {
   public:
      //int* ptr;  //generates error in building
      int ptr;
  };
  
  __kernel void vector_add(__global int *d,__global int *b,__global int  *c,__global class A *obj)
  {
   size_t id=get_global_id(0);
  
  c[id]=d[id]+b[id];
  
  __global int *p=(__global int *)obj-&gt;ptr;
  printf("kernel *p= %d p= %d  obj-&gt;ptr= %d \n",*(p),p,obj-&gt;ptr); 
  }
  </code></pre>