【问题标题】:OpenCL clCreateProgramFromSource() segfaults with address of single string #includedOpenCL clCreateProgramFromSource() 段错误,地址为单个字符串#included
【发布时间】:2021-12-30 14:57:32
【问题描述】:

使用字符串 arg 在单个源上调用 clCreateProgramFromSource 会设置 #included 字符数组段错误的地址,但将数组作为字符指针数组的元素传入。

#include "prog_src.h"
const char *cl_srcs[1];
cl_srcs[0] = prog_src_cl;

cl.prog = clCreateProgramWithSource(cl.context, 1, cl_srcs, NULL, &err);
puts("test0");
cl.prog = clCreateProgramWithSource(cl.context, 1, &prog_src_cl, NULL, &err);
puts("test1");

“test0”将打印,然后程序段错误,我不确定为什么它不适用于第二个版本,prog_src_cl 是文件#included 中的无符号字符*。

任何帮助都将不胜感激,因为我似乎无法理解为什么如此看似微不足道的事情无法按预期工作。

【问题讨论】:

    标签: c pointers opencl


    【解决方案1】:

    函数clCreateProgramWithSource 期望通过参数const char** strings 传递源。你说prog_src_cl 是一个字符数组。这意味着&proc_src_cl 的类型为const char (*)[N]。 (指向 N 元素常量字符数组的指针,其中 N 可能是用于初始化数组的字符串的长度,加上 1 表示 nul 终止字符。)这与 const char** 不同,您不能安全地转换他们之间!

    我建议您在 C 编译器中打开更多类型检查选项,例如将 -Wextra 传递给 clang 或 gcc。例如,clang 在-Wincompatible-pointer-types 警告中捕获了这个编程错误,该警告包含在-Wextra 中。

    【讨论】:

    • 谢谢,我相信 gcc 确实警告过我,但我把它排版了,因为我认为 (*)[N]** 没有任何不同。我猜你知道的越多。奇怪的行为。
    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    相关资源
    最近更新 更多