title: Unable to print device variable value within kernel in CUDA
body: <p>I am trying to understand how pointers function in CUDA. </p>
  
  <p>Given below is a simple program which assigns certain value to a variable allocated in the device. 
  (I'm using CUDA toolkit 8.0 with NVIDIA Quadro K2000 Graphics card)</p>
  
  <p>When I print the value within the kernel using printf(), it displays a wrong value. </p>
  
  <p>However, when I do a cudaMemcopy from host function and then print from the host function, it displays the correct value...</p>
  
  <pre><code>//CODE...
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include&lt;cuda.h&gt;
  #include&lt;cuda_runtime.h&gt;
  #define N 3
  
  __global__ void tempker(int *jk,int value)
  {
      (*jk) = value*2;
      printf("displayed from inside the kernel :\nvalue of jk = %d\nvalue of *jk = %d\n",jk,*jk);
  
  }
  int tempfunc(int *kp)
  {
      int *jk = NULL,*lm=NULL;
      lm = (int *)(malloc(sizeof(int)));
      *lm = 150;
      cudaError_t err = cudaSuccess;
      cudaMalloc((void**)&amp;jk, sizeof(int));
      printf("jk pointer after cudaMalloc: displayed from host = %d\n",jk);
      tempker&lt;&lt;&lt;1,1&gt;&gt;&gt;(jk,150);
      err = cudaGetLastError();//brief Returns the last error from a runtime call
      cudaDeviceSynchronize();
  
      err = cudaMemcpy(lm, jk, (sizeof(int)), cudaMemcpyDeviceToHost);
      if (err != cudaSuccess)
      {
          fprintf(stderr, "Failed to copy jk from device to host (error code %s)!\n", cudaGetErrorString(err));
          exit(EXIT_FAILURE);
      }
      printf("Displayed in host function after memcopy: value of *lm = *jk = %d\n",*lm);
      cudaFree(jk);
      err = cudaMalloc((void**)&amp;kp, sizeof(int));
      if (err != cudaSuccess)
      {
          fprintf(stderr, "Failed to allocate device kp (error code %s)!\n", cudaGetErrorString(err));
          exit(EXIT_FAILURE);
      }
      tempker&lt;&lt;&lt;1,1&gt;&gt;&gt;(kp,(N*N*N));
      err = cudaGetLastError();//brief Returns the last error from a runtime call
      cudaDeviceSynchronize();
  
      err = cudaMemcpy(lm, kp, (sizeof(int)), cudaMemcpyDeviceToHost);
      if (err != cudaSuccess)
      {
          fprintf(stderr, "Failed to copy kp from device to host (error code %s)!\n", cudaGetErrorString(err));
          exit(EXIT_FAILURE);
      }
      printf("Displayed in host function after memcopy: value of *lm = *kp = %d\n",*lm);
      cudaFree(kp);
      free(lm);
      return 100;
  }
  
  int main(){
  
      int *kp = NULL;
      printf("tempfunc(): return value = %d\n",tempfunc(kp));
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>jk pointer after cudaMalloc: displayed from host = 13238272
  displayed from inside the kernel :
  value of jk = 13238272
  value of *jk = 9
  Displayed in host function after memcopy: value of *lm = *jk = 300
  displayed from inside the kernel :
  value of jk = 13238272
  value of *jk = 9
  Displayed in host function after memcopy: value of *lm = *kp = 54
  tempfunc(): return value = 100
  </code></pre>
  
  <p>The Question is: 
  <strong>Is it possible to print value of a variable allocated in device within the kernel?</strong></p>