Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to pointer syntax problem

Tags:

c++

cuda

Lets say I have the following:

void init_gpu(cuComplex* d_hhBuff)
{   
    cutilSafeCall(cudaMalloc((void **)&d_hhBuff, memsize));
}

and I call it with something like

cuComplex *my_buff;
init_gpu(my_buff);

Well, when init_gpu returns, it is NOT pointing to the device memory that cudaMalloc allocated.

How do I modify this so that the caller of init_gpu will have my_buff pointing to the modified d_hhBuff that cudaMalloc creates?

like image 650
Derek Avatar asked Sep 18 '26 11:09

Derek


2 Answers

The problem is that you are passing the pointer by value. Change the function header to

void init_gpu(cuComplex *& d_hhBuff)
like image 161
Daniel Avatar answered Sep 21 '26 03:09

Daniel


Your d_hhBuf is a local copy. What you should do is pass the pointer by reference:

void init_gpu(cuComplex * & d_hhBuff)
like image 23
Kerrek SB Avatar answered Sep 21 '26 04:09

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!