我用 OpenCL C 编写了一段代码来列出所有可用的平台。
int main()
{
cl_context context;
cl_platform_id* platforms;
cl_device_id* devices;
cl_uint platformcount;
cl_int ret;
clGetPlatformIDs(2,NULL,&platformcount);
clGetPlatformIDs(platformcount,platforms,NULL);
/*if(ret==CL_SUCCESS)
{
printf("\nNumber of platforms found=%d\n",platformcount);
}*/
return 0;
}
这会导致核心被转储(段错误(核心转储))。
$ gcc -lOpenCL a.c -o a && ./a
Segmentation fault (core dumped)
但是,如果我注释掉 ret 声明,则代码可以正常编译。
int main()
{
cl_context context;
cl_platform_id* platforms;
cl_device_id* devices;
cl_uint platformcount;
//cl_int ret;
clGetPlatformIDs(2,NULL,&platformcount);
clGetPlatformIDs(platformcount,platforms,NULL);
/*if(ret==CL_SUCCESS)
{
printf("\nNumber of platforms found=%d\n",platformcount);
}*/
return 0;
}
为什么会发生这种情况?
最佳答案
本次通话
clGetPlatformIDs(platformcount,platforms,NULL);
写入platforms
指向的位置,但platforms
尚未初始化为指向任何地方,因此调用调用UB。
关于c - 在 OpenCL C 中声明 cl_uint 变量会导致段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043892/