为什么 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() 使用指向指针的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 cudaMalloc() 使用指向指针的指针?

基础教程推荐