title: Pointer arithmetic on void* pointers
body: <p>I am using the CUDA API / cuFFT API. In order to move data from host to GPU I am usign the cudaMemcpy functions. I am using it like below. len is the amount of elements on dataReal and dataImag.  </p>
  
  <pre><code>void foo(const double* dataReal, const double* dataImag, size_t len)
  {
      cufftDoubleComplex* inputData;
      size_t allocSizeInput = sizeof(cufftDoubleComplex)*len;
      cudaError_t allocResult = cudaMalloc((void**)&amp;inputData, allocSizeInput);
  
      if (allocResult != cudaSuccess) return;
  
      cudaError_t copyResult;
  
      coypResult = cudaMemcpy2D(static_cast&lt;void*&gt;(inputData),
                                2 * sizeof (double),
                                static_cast&lt;const void*&gt;(dataReal),
                                sizeof(double),
                                sizeof(double),
                                len,
                                cudaMemcpyHostToDevice);
  
      coypResult &amp;= cudaMemcpy2D(static_cast&lt;void*&gt;(inputData) + sizeof(double),
                                2 * sizeof (double),
                                static_cast&lt;const void*&gt;(dataImag),
                                sizeof(double),
                                sizeof(double),
                                len,
                                cudaMemcpyHostToDevice);
  
      //and so on.
  }
  </code></pre>
  
  <p>I am aware, that pointer arithmetic on void pointers is actually not possible. the second cudaMemcpy2D does still work though. I still get a warning by the compiler, but it works correctly.</p>
  
  <p>I tried using static_cast&lt; char* > but that doesn't work as cuffDoubleComplex* cannot be static casted to char*. </p>
  
  <p>I am a bit confused why the second cudaMemcpy with the pointer arithmetic on void is working, as I understand it shouldn't. Is the compiler implicitly assuming that the datatype behind void* is one byte long? </p>
  
  <p>Should I change something there? Use a reinterpret_cast&lt; char* >(inputData) for example?</p>
  
  <p>Also during the allocation I am using the old C-style (void**) cast. I do this because I am getting a "invalid static_cast from cufftDoubleComplex** to void**". Is there another way to do this correctly?</p>
  
  <p>FYI: <a href="http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g3a58270f6775efe56c65ac47843e7cee" rel="nofollow">Link to cudaMemcpy2D Doc</a></p>
  
  <p><a href="http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g37d37965bfb4803b6d4e59ff26856356" rel="nofollow">Link to cudaMalloc Doc</a></p>