指针到指针的语法问题
可以说我有以下内容:
void init_gpu(cuComplex* d_hhBuff)
{
cutilSafeCall(cudaMalloc((void **)&d_hhBuff, memsize));
}
我用类似的方式调用它
cuComplex *my_buff;
init_gpu(my_buff);
,当 init_gpu 返回时,它并不指向 cudaMalloc 分配的设备内存。
如何修改它,以便 init_gpu 的调用者将 my_buff 指向 cudaMalloc 创建的修改后的 d_hhBuff?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您正在按值传递指针。将函数头更改为
The problem is that you are passing the pointer by value. Change the function header to
您的
d_hhBuf
是本地副本。你应该做的是通过引用传递指针:Your
d_hhBuf
is a local copy. What you should do is pass the pointer by reference:假设您 typedef:
并编写一个函数:
并调用它:
通过查看 call 和函数(并且不知道 typedef),您会推断出什么?您可能会说,
ptr
(cptr
) 是按值传递的,而不是按引用/指针传递的,并且不会更改。现在考虑这个调用:
这与上面完全相同,您实际上传递的是
ComplexPointer
类型 - 它只是按值传递。最后了解更改后的签名:
它仍然是相同的 - 您通过 VALUE 传递 type
cuComplex*
,而不是通过引用!Assume you typedef:
And write a function:
And call it:
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:
This is exactly same as above, you are actually passing type
ComplexPointer
- which just being passed by value.Finally understand the changed signature:
And it is still the same - you are pass the type
cuComplex*
by VALUE, and not by reference!