指针到指针的语法问题

发布于 2024-12-01 05:46:20 字数 355 浏览 0 评论 0原文

可以说我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘亓 2024-12-08 05:46:20

问题是您正在按值传递指针。将函数头更改为

void init_gpu(cuComplex *& d_hhBuff)

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

void init_gpu(cuComplex *& d_hhBuff)
享受孤独 2024-12-08 05:46:20

您的 d_hhBuf 是本地副本。你应该做的是通过引用传递指针:

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)
婴鹅 2024-12-08 05:46:20

假设您 typedef:

typedef cuComplex* ComplexPointer;

并编写一个函数:

void ChangeComplex(ComplexPointer ptr)
{
   ptr = new cuComplex;
}

并调用它:

ComplexPointer cptr;
ChangeComplex(cptr);

通过查看 call 和函数(并且知道 typedef),您会推断出什么?您可能会说,ptr (cptr) 是按值传递的,而不是按引用/指针传递的,并且不会更改。

现在考虑这个调用:

cuComplex* ptr;
ChangeComplex(cptr);

这与上面完全相同,您实际上传递的是ComplexPointer类型 - 它只是按值传递。

最后了解更改后的签名:

void ChangeComplex(cuComplex*ptr);

它仍然是相同的 - 您通过 VALUE 传递 type cuComplex* ,而不是通过引用!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文