CUDA程序抛出内存泄漏错误
我不明白为什么下面的简单代码在 c[0] = d 处失败
void test(char **a){
char **c;
cudaMemcpy(c,a, sizeof(char*), cudaMemcpyDeviceToHost);
char temp[2];
for(int i = 0 ; i< 2; i++){
temp[i ] = temp[i] & 0 ;
}
char *d;
cudaMalloc((void**)&d, 2*sizeof(char));
cudaMemcpy(d, temp, 2 * sizeof(char), cudaMemcpyHostToDevice);
c[0] = d;
}
void main(){
char **a ;
cudaMalloc((void**)&a, sizeof( char*));
test(a);
}
I do not understand why the below simple code is failing at c[0] = d
void test(char **a){
char **c;
cudaMemcpy(c,a, sizeof(char*), cudaMemcpyDeviceToHost);
char temp[2];
for(int i = 0 ; i< 2; i++){
temp[i ] = temp[i] & 0 ;
}
char *d;
cudaMalloc((void**)&d, 2*sizeof(char));
cudaMemcpy(d, temp, 2 * sizeof(char), cudaMemcpyHostToDevice);
c[0] = d;
}
void main(){
char **a ;
cudaMalloc((void**)&a, sizeof( char*));
test(a);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记为
char **c
分配内存。因此,在导致错误的行中,c
是一个“死指针”,即等于 NULL 或引用不属于您的程序的内存部分。换句话说,c
指向一个空的、未分配的 C 字符串数组。寻址c[0]
元素(应该是数组中的第一个字符串,该字符串不存在)是非法的,并且会给您带来分段错误,因为您正在尝试写入某些内容(< code>d) 到不属于您的位置。解决方案是在写入
c[0]
之前分配内存:记住如何调用 CudaMalloc() 在 GPU 上为
a
和d
分配内存代码>变量?您只需对 c 执行相同的操作,但在主 RAM 中(即在主机上)希望它有帮助。
You forgot to allocate memory for
char **c
. Therefore, in the error-causing line,c
is a "dead pointer", i.e. either equal to NULL or referring to a section of memory that is not owned by your program. In other words,c
points to an empty, unallocated array of C strings. Adressing thec[0]
element (supposedly the first string in the array, which does not exist) is illegal and gives you a segmentation fault, because you're trying to write something (the value ofd
) into a location which you do not own.The solution is to allocate memory before writing into
c[0]
:Remember how you called CudaMalloc() to allocate memory on the GPU for
a
andd
variables? You just have to do the same forc
, but in the main RAM (i.e. on the host)Hope it helps.
首先,从代码中根本不清楚您要做什么,因此也许将其添加到 Q 中会有所帮助。其次,您从编译器收到的错误消息到底是什么?
至于失败,可能是编译器不喜欢您尝试使用
[]
访问c
,因为它没有声明为数组。我知道原则上它应该可以工作,但尝试使用 *c = d。这有帮助吗?Ps 就我个人而言,我总是使用术语
devFoo
来表示任何指向设备内存的指针,这样我就不会感到困惑,在较大的项目中,很容易忘记是否a
,b
或c
指向主机或设备内存。Firstly, from the code it's not at all clear what you are trying to do so perhaps adding this to the Q would help. Secondly what exactly is the error message you are getting from the compiler?
As for the failure, it could be that the compiler doesn't like you trying to access
c
using[]
since it was not declared as an array. I know in principle it should work but try using*c = d
. Does this help?P.s. Personally I always use the nomenclature
devFoo
for any pointers to device memory so I don't get confused, in a larger project it is easy to lose track of whethera
,b
orc
point to host or device memory.