Pointer to pointer syntax problem
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 wil开发者_StackOverflow社区l have my_buff pointing to the modified d_hhBuff that cudaMalloc creates?
The problem is that you are passing the pointer by value. Change the function header to
void init_gpu(cuComplex *& d_hhBuff)
Your d_hhBuf
is a local copy. What you should do is pass the pointer by reference:
void init_gpu(cuComplex * & d_hhBuff)
Assume you typedef:
typedef cuComplex* ComplexPointer;
And write a function:
void ChangeComplex(ComplexPointer ptr)
{
ptr = new cuComplex;
}
And call it:
ComplexPointer cptr;
ChangeComplex(cptr);
What would you deduce by looking at call and the function (and not knowing the typedef) ? You would say, the ptr
(cptr
) is passed by value, and not by reference/pointer and won't change.
Now consider this call:
cuComplex* ptr;
ChangeComplex(cptr);
This is exactly same as above, you are actually passing type ComplexPointer
- which just being passed by value.
Finally understand the changed signature:
void ChangeComplex(cuComplex*ptr);
And it is still the same - you are pass the type cuComplex*
by VALUE, and not by reference!
精彩评论