【问题标题】:Declaring a cl_uint variable in OpenCL C leads to Segmentation fault (core dumped)在 OpenCL C 中声明 cl_uint 变量会导致分段错误(核心转储)
【发布时间】:2015-06-25 07:28:20
【问题描述】:

我用 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;
}

这会导致核心被转储(Segmentation fault (core dumped))。

$ 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;
}

为什么会这样?

【问题讨论】:

  • 要突出显示代码,请将您的代码环绕在一对 ` 之间
  • 哦。我用 ' 而不是 `。

标签: c linux parallel-processing opencl gpgpu


【解决方案1】:

这个电话

clGetPlatformIDs(platformcount,platforms,NULL);

写入platforms 指向的位置,但platforms 尚未初始化为指向任何位置,因此调用调用了UB。

【讨论】:

  • @alk 你是对的。我在第一次和第二次通话之间错过了
  • 但是为什么这不会导致任何错误? '//cl_int ret; clGetPlatformIDs(1,NULL,&platformcount); //平台 = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformcount); clGetPlatformIDs(platformcount,platforms,NULL); /*if(ret==CL_SUCCESS) { printf("\n找到的平台数量=%d\n",platformcount); }*/ printf("\n代码幸存!\n");返回0; }' @alk
  • @krish94:调用调用 U(ndefined)B(ehaviour)。这意味着任何事情都可能发生:从通过重启机器到违反分段到什么都没有。
  • 有人可以告诉我如何在评论中突出显示代码!
猜你喜欢
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 2022-08-23
  • 2016-09-11
  • 2011-05-31
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
相关资源
最近更新 更多