CUDA程序抛出内存泄漏错误

发布于 2024-11-29 12:49:02 字数 456 浏览 1 评论 0原文

我不明白为什么下面的简单代码在 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 技术交流群。

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

发布评论

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

评论(2

ゃ人海孤独症 2024-12-06 12:49:02

您忘记为 char **c 分配内存。因此,在导致错误的行中,c 是一个“死指针”,即等于 NULL 或引用不属于您的程序的内存部分。换句话说,c 指向一个空的、未分配的 C 字符串数组。寻址 c[0] 元素(应该是数组中的第一个字符串,该字符串不存在)是非法的,并且会给您带来分段错误,因为您正在尝试写入某些内容(< code>d) 到不属于您的位置。

解决方案是在写入 c[0] 之前分配内存:

c = new char *; //or "c = new char [5]"  if you want it to hold more strings
c[0] = d;

记住如何调用 CudaMalloc() 在 GPU 上为 ad 分配内存代码>变量?您只需对 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 the c[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 of d) into a location which you do not own.

The solution is to allocate memory before writing into c[0]:

c = new char *; //or "c = new char [5]"  if you want it to hold more strings
c[0] = d;

Remember how you called CudaMalloc() to allocate memory on the GPU for a and d variables? You just have to do the same for c, but in the main RAM (i.e. on the host)

Hope it helps.

就此别过 2024-12-06 12:49:02

首先,从代码中根本不清楚您要做什么,因此也许将其添加到 Q 中会有所帮助。其次,您从编译器收到的错误消息到底是什么?

至于失败,可能是编译器不喜欢您尝试使用 [] 访问 c ,因为它没有声明为数组。我知道原则上它应该可以工作,但尝试使用 *c = d。这有帮助吗?

Ps 就我个人而言,我总是使用术语 devFoo 来表示任何指向设备内存的指针,这样我就不会感到困惑,在较大的项目中,很容易忘记是否 abc 指向主机或设备内存。

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 whether a,b or c point to host or device memory.

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