Can I alias two device arrays in the host side?

The question is exactly depicted in the title. Consider an Example below:

cudaMalloc(d_a,5*sizeof(int));
cudaMalloc(d_b,5*sizeof(int));
d_a = d_b;

Is the above-mentioned behavior legal? Or can I only alias the two device arrays in the device-side kernel code?

int* d_a;
int* d_b;
cudaMalloc(&d_a ,5*sizeof(int));
cudaMalloc(&d_b, 5*sizeof(int));
d_a = d_b;

That leaks the memory first allocated of d_a.

Hi, Can you please elaborate?

That is not a cuda-specific question. By assignment you loose the information about the starting address of the first buffer, so you can never free it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.