为什么 cudaMalloc() 使用指针指向指针?

Why does cudaMalloc() use pointer to pointer?(为什么 cudaMalloc() 使用指针指向指针?)
本文介绍了为什么 cudaMalloc() 使用指针指向指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

例如,cudaMalloc((void**)&device_array, num_bytes);

这个问题之前已经问过,得到的答复是因为 cudaMalloc 返回错误代码",但我不明白 - 双指针与返回错误代码有什么关系?为什么简单的指针不能完成这项工作?

This question has been asked before, and the reply was "because cudaMalloc returns an error code", but I don't get it - what has a double pointer got to do with returning an error code? Why can't a simple pointer do the job?

如果我写

cudaError_t catch_status;
catch_status = cudaMalloc((void**)&device_array, num_bytes);

错误代码会放在catch_status中,返回一个简单的指向分配的GPU内存的指针就足够了,不是吗?

the error code will be put in catch_status, and returning a simple pointer to the allocated GPU memory should suffice, shouldn't it?

推荐答案

在 C 中,数据可以通过值或通过 模拟传递引用(即通过指向数据的指针).按值是一种单向方法,按指针允许函数与其调用环境之间的双向数据流.

In C, data can be passed to functions by value or via simulated pass-by-reference (i.e. by a pointer to the data). By value is a one-way methodology, by pointer allows for two-way data flow between the function and its calling environment.

当一个数据项通过函数参数列表传递给一个函数,并且该函数希望修改原始数据项以便修改后的值显示在调用环境中时,正确的C方法是传递通过指针的数据项.在 C 中,当我们传递指针时,我们获取要修改的项的地址,创建一个指针(在这种情况下可能是指向指针的指针)并将地址传递给函数.这允许函数在调用环境中修改原始项目(通过指针).

When a data item is passed to a function via the function parameter list, and the function is expected to modify the original data item so that the modified value shows up in the calling environment, the correct C method for this is to pass the data item by pointer. In C, when we pass by pointer, we take the address of the item to be modified, creating a pointer (perhaps a pointer to a pointer in this case) and hand the address to the function. This allows the function to modify the original item (via the pointer) in the calling environment.

通常malloc返回一个指针,我们可以在调用环境中使用赋值的方式将这个返回值赋值给想要的指针.对于cudaMalloc,CUDA 设计者选择使用返回值来携带错误状态而不是指针.因此,调用环境中指针的设置必须通过通过引用(即通过指针)传递给函数的参数之一进行.由于我们要设置的是指针值,我们必须获取指针的地址(创建指向指针的指针)并将该地址传递给cudaMalloc函数.

Normally malloc returns a pointer, and we can use assignment in the calling environment to assign this returned value to the desired pointer. In the case of cudaMalloc, the CUDA designers chose to use the returned value to carry an error status rather than a pointer. Therefore the setting of the pointer in the calling environment must occur via one of the parameters passed to the function, by reference (i.e. by pointer). Since it is a pointer value that we want to set, we must take the address of the pointer (creating a pointer to a pointer) and pass that address to the cudaMalloc function.

这篇关于为什么 cudaMalloc() 使用指针指向指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Could I ever want to access the address zero?(我可以想访问地址零吗?)
C++ Access derived class member from base class pointer(C++ 从基类指针访问派生类成员)
Weird Behaviour with const_cast(const_cast 的奇怪行为)
Are pointer variables just integers with some operators or are they quot;symbolicquot;?(指针变量只是带有某些运算符的整数还是“符号?)
Modifying a char *const string(修改 char *const 字符串)
Modifying a const int in C++(修改 C++ 中的 const int)