# seems that cuda doesn't support pointer to pointer problem report

**URL:** <https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305>\
**Category:** CUDA Programming and Performance\
**Created:** [March 1, 2007, 2:10am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305 "2007-03-01T02:10:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yk\_cadcg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yk\_cadcg](https://forums.developer.nvidia.com/u/yk_cadcg)\
**Post date:** [March 1, 2007, 2:10am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/1 "2007-03-01T02:10:56Z")

</div>

Hi, it seems that cuda doesn’t support pointer to pointers, like:  
int\*\* a;  
cudaMalloc(&a, sizeof(int\*) \* N));  
for(int i = 0; i \< N; ++i)  
cudaMalloc(&a[i],size)); //memory violation here

above psuedocode can’t pass Device, we have to use continuous 1d array or cudaMalloc2D.

Any advice? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![yk\_cadcg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yk\_cadcg](https://forums.developer.nvidia.com/u/yk_cadcg)\
**Post date:** [March 1, 2007, 5:44am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/2 "2007-03-01T05:44:29Z")

</div>

Anybody met this issue? Waiting for reply hurrily…

> [@](#):
>
> Hi, it seems that cuda doesn’t support pointer to pointers, like:
> 
> int\*\* a;
> 
> cudaMalloc(&a, sizeof(int\*) \* N));
> 
> for(int i = 0; i \< N; ++i)
> 
> ```
> cudaMalloc(&a[i],size));  //memory violation here
> 
> ```
> 
> above psuedocode can’t pass Device, we have to use continuous 1d array or cudaMalloc2D.
> 
> Any advice? Thanks a lot!
> 
> [snapback]165385[/snapback]

---

<div class="post-metadata">

**Author:** ![javier1](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@javier1](https://forums.developer.nvidia.com/u/javier1)\
**Post date:** [March 1, 2007, 9:39am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/3 "2007-03-01T09:39:55Z")

</div>

> [@](#):
>
> Hi, it seems that cuda doesn’t support pointer to pointers, like:
> 
> int\*\* a;
> 
> cudaMalloc(&a, sizeof(int\*) \* N));
> 
> for(int i = 0; i \< N; ++i)
> 
> ```
> cudaMalloc(&a[i],size));  //memory violation here
> 
> ```
> 
> above psuedocode can’t pass Device, we have to use continuous 1d array or cudaMalloc2D.
> 
> Any advice? Thanks a lot!
> 
> [snapback]165385[/snapback]

I didn’t say anything :P

Anyway, I think CUDA does not support pointers, so I suppose you will have to replace pointers by indexes or something similar. You have to keep in mind that after cudaMalloc( &a, … ), a is “pointing” to device memory, not host memory. And as soon as you dereference a, you are doing something that has no sense, \*a is not memory in host space.

If you just need an array of device memory chunks, the first malloc must be a regular one. But keep in mind that you will not be able to dereference it in the device, you can use it for things like:

cudaDeviceFunction\<\<\<…\>\>\>( a[1], a[2], … );

not as a parameter by itself.

---

<div class="post-metadata">

**Author:** ![Cyril\_Zeller](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Cyril\_Zeller](https://forums.developer.nvidia.com/u/Cyril_Zeller)\
**Post date:** [March 1, 2007, 10:16am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/4 "2007-03-01T10:16:14Z")

</div>

You cannot dereference a device pointer in host code (see last sentence of Section 4.2.2.4). In the same way, you cannot have cudaMalloc write to a location pointed to by a device pointer.

You need to explicitly copy to device memory with something like this:

```auto
int** a; 

cudaMalloc(&a, sizeof(int*) * N));

int* ha[N];

for(int i = 0; i < N; ++i) 

       cudaMalloc(&ha[i],size));

cudaMemcpy(a, ha, sizeof(a), cudaMemcpyHostToDevice);

```

---

<div class="post-metadata">

**Author:** ![yk\_cadcg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yk\_cadcg](https://forums.developer.nvidia.com/u/yk_cadcg)\
**Post date:** [March 2, 2007, 6:13am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/5 "2007-03-02T06:13:17Z")

</div>

Thank you very much! I will try.

> [@](#):
>
> You cannot dereference a device pointer in host code (see last sentence of Section 4.2.2.4). In the same way, you cannot have cudaMalloc write to a location pointed to by a device pointer.
> 
> You need to explicitly copy to device memory with something like this:
> 
> ```auto
> int** a; 
> 
> cudaMalloc(&a, sizeof(int*) * N));
> 
> int* ha[N];
> 
> for(int i = 0; i < N; ++i) 
> 
>       cudaMalloc(&ha[i],size));
> 
> cudaMemcpy(a, ha, sizeof(a), cudaMemcpyHostToDevice);
> 
> ```
> 
> [snapback]165494[/snapback]

---

<div class="post-metadata">

**Author:** ![jhanweck](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jhanweck](https://forums.developer.nvidia.com/u/jhanweck)\
**Post date:** [May 24, 2007, 9:40pm UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/6 "2007-05-24T21:40:39Z")

</div>

I have tried this (using floats), but in the kernel, the compiler appears confused about how to dereference the pointer.

Can you tell me what’s wrong below, or post the kernel code corresponding to your host code?

Thanks!

```auto
// one row of the array per thread block...

__global__ void kernelTestP2P(float **g_array, float *g_Out) 

{

    unsigned int bid = blockIdx.x;

    unsigned int tid = threadIdx.x;

   // get the pointer to this thread block's row

     __shared__ float *g_row;    // or should this be:  float *g_row; ....

    if (tid == 0)          // ... and eliminate this line???  neither seems to work...

        g_row = g_array[bid];

    __syncthreads();

   shmem[tid] = g_row[tid];

    __syncthreads();

```

Can you provide an example of the kernel code?

> [@](#):
>
> You cannot dereference a device pointer in host code (see last sentence of Section 4.2.2.4). In the same way, you cannot have cudaMalloc write to a location pointed to by a device pointer.
> 
> You need to explicitly copy to device memory with something like this:
> 
> ```auto
> int** a; 
> 
> cudaMalloc(&a, sizeof(int*) * N));
> 
> int* ha[N];
> 
> for(int i = 0; i < N; ++i) 
> 
>       cudaMalloc(&ha[i],size));
> 
> cudaMemcpy(a, ha, sizeof(a), cudaMemcpyHostToDevice);
> 
> ```
> 
> [snapback]165494[/snapback]

---

<div class="post-metadata">

**Author:** ![BARRACUDA](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BARRACUDA](https://forums.developer.nvidia.com/u/BARRACUDA)\
**Post date:** [June 4, 2007, 2:30pm UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/7 "2007-06-04T14:30:34Z")

</div>

What about using cudaMalloc2D? Although I haven’t personally been able to do much with it, it seems like the thing that you’re looking for.

---

<div class="post-metadata">

**Author:** ![AndreasBuhr](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/andreasbuhr/32/52550_2.png) [@AndreasBuhr](https://forums.developer.nvidia.com/u/AndreasBuhr)\
**Post date:** [June 5, 2007, 11:56am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/8 "2007-06-05T11:56:58Z")

</div>

> [@](#):
>
> ```auto
> // one row of the array per thread block...
> 
> __global__ void kernelTestP2P(float **g_array, float *g_Out) 
> 
> {
> 
>    unsigned int bid = blockIdx.x;
> 
>    unsigned int tid = threadIdx.x;
> 
>    // get the pointer to this thread block's row
> 
>     __shared__ float *g_row;    // or should this be:  float *g_row; ....
> 
>    if (tid == 0)          // ... and eliminate this line???  neither seems to work...
> 
>        g_row = g_array[bid];
> 
>    __syncthreads();
> 
>    shmem[tid] = g_row[tid];
> 
>    __syncthreads();
> 
> ```

I think g\_row is a variable in shared memory in your code. When accessing g\_row[tid], the compiler probably tries to access shared memory.

You should have a look at your .ptx file. There is probably something like

ld.global.f32

st.shared.f32

when loading something from global memory to shared memory. Maybe in your ptx file is something like

ld.shared.f32

st.shared.f32

then you know, where your Problem is ;-)

---

<div class="post-metadata">

**Author:** ![Cyril\_Zeller](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Cyril\_Zeller](https://forums.developer.nvidia.com/u/Cyril_Zeller)\
**Post date:** [June 5, 2007, 3:36pm UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/9 "2007-06-05T15:36:12Z")

</div>

The code I wrote earlier had a typo; here’s the right code for the copy to work correctly:

> [@](#):
>
> int\*\* a;
> 
> cudaMalloc(&a, sizeof(int\*) \* N));
> 
> int\* ha[N];
> 
> for(int i = 0; i \< N; ++i)
> 
> cudaMalloc(&ha[i],size));
> 
> cudaMemcpy(a, ha, sizeof(ha), cudaMemcpyHostToDevice);

---

<div class="post-metadata">

**Author:** ![ismailfaruqi](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/ismailfaruqi/32/12043_2.png) [@ismailfaruqi](https://forums.developer.nvidia.com/u/ismailfaruqi)\
**Post date:** [November 17, 2011, 5:06am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/10 "2011-11-17T05:06:56Z")

</div>

> [@](#):
>
> The code I wrote earlier had a typo; here’s the right code for the copy to work correctly:

wow I dont believe facing a problem solved 4 years ago!!!

this saves me from headbanging further LOL

---

<div class="post-metadata">

**Author:** ![lclin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@lclin](https://forums.developer.nvidia.com/u/lclin)\
**Post date:** [January 6, 2012, 8:33am UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/11 "2012-01-06T08:33:16Z")

</div>

> [@](#):
>
> wow I dont believe facing a problem solved 4 years ago!!!
> 
> this saves me from headbanging further LOL

Now, is there a new easier way to do this problem since 4 years pasted !

---

<div class="post-metadata">

**Author:** ![Araz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Araz](https://forums.developer.nvidia.com/u/Araz)\
**Post date:** [March 29, 2012, 3:43pm UTC](https://forums.developer.nvidia.com/t/seems-that-cuda-doesnt-support-pointer-to-pointer-problem-report/305/12 "2012-03-29T15:43:46Z")

</div>

This page helped me a lot. so I just add a simple example to show clearly how we can use pointer to pointers in cuda.  
//////////////////////////////P2P.cu//////////////  
#include   
#include   
#include   
#include   
#include   
#include \<stddef.h\>  
#include \<cuda.h\>  
#include \<math\_functions.h\>  
static void HandleError( cudaError\_t err,const char \*file, int line ) {  
if (err != cudaSuccess) {  
printf( “%s in %s at line %d\n”, cudaGetErrorString( err ),  
file, line );  
exit( EXIT\_FAILURE );  
}  
}  
#define HANDLE\_ERROR( err ) (HandleError( err, **FILE** , **LINE** ))  
float Randomizer () { return (float)rand()/(float)RAND\_MAX; }

**global** void kernelTestP2P(float \*\*in, float \*\*out)  
{  
unsigned int bid = blockIdx.x;  
unsigned int tid = threadIdx.x;

```
// get the pointer to this thread block's row
extern __shared__ float shmem[];

shmem[tid] = in[bid][tid];
__syncthreads();
out[bid][tid]=sinf(shmem[tid]);

```

}

int main(){  
int B=14,N=32;  
srand ( unsigned ( time(NULL) ) );

float \*\*in\_dd,\*\*in\_hd,\*\*in\_hh;

//alocate 1st part, B count of float\*  
in\_hd=(float\*\*)malloc(sizeof(float\*)_B);  
in\_hh=(float_\*)malloc(sizeof(float\*)_B);  
HANDLE\_ERROR(cudaMalloc(&in\_dd, sizeof(float_) \* B));  
std::cout\<\<“OK\n”;  
//alocate 2nd part, N count of float  
for(int i = 0; i \< B; ++i) {  
HANDLE\_ERROR(cudaMalloc((void\*\*)&in\_hd[i],N_sizeof(float)));  
in\_hh[i]=(float_)malloc(N_sizeof(float));//allocate or connect the input data to it  
std::generate\_n(in\_hh[i],N,Randomizer);  
HANDLE\_ERROR(cudaMemcpy(in\_hd[i],in\_hh[i],sizeof(float)N,cudaMemcpyHostToDevice));  
}  
//copy float part from hd to dd  
HANDLE\_ERROR(cudaMemcpy(in\_dd, in\_hd, sizeof(float_)\*B, cudaMemcpyHostToDevice));

float \*\*out\_dd,\*\*out\_hd,\*\*out\_hh;

out\_hh=(float\*\*)malloc(sizeof(float\*)_B);  
out\_hd=(float_\*)malloc(sizeof(float\*) \* B);  
HANDLE\_ERROR(cudaMalloc(&out\_dd, sizeof(float\*) \* B));

for(int i=0;i\<B;i++){  
HANDLE\_ERROR(cudaMalloc((void\*\*)&out\_hd[i],N_sizeof(float)));  
out\_hh[i]=(float_)malloc(N_sizeof(float));//just alocate, NO input  
}  
HANDLE\_ERROR(cudaMemcpy(out\_dd,out\_hd,B_sizeof(float\*),cudaMemcpyHostToDevice));

size\_t shMemSize=N\*sizeof(float);  
dim3 GridDim=dim3(B,1,1),BlockDim=dim3(N,1,1);

kernelTestP2P\<\<\<GridDim,BlockDim,shMemSize\>\>\>(in\_dd,out\_dd);

for(int i=0;i\<B;i++){  
HANDLE\_ERROR(cudaMemcpy(out\_hh[i], out\_hd[i], sizeof(float)\*N, cudaMemcpyDeviceToHost));  
HANDLE\_ERROR(cudaFree(in\_hd[i]));  
HANDLE\_ERROR(cudaFree(out\_hd[i]));  
}  
HANDLE\_ERROR(cudaFree(in\_dd));  
HANDLE\_ERROR(cudaFree(out\_dd));

std::ofstream f\_out;  
f\_out.open(“output.txt”);

for(int i=0;i\<B;i++) {  
for(int j=0;j\<N;j++) f\_out\<\<out\_hh[i][j]\<\<" “;  
f\_out\<\<”\n";  
free(out\_hh[i]);  
free(in\_hh[i]);  
}  
free(out\_hh);  
free(in\_hh);  
return 0;  
}
