Home > OS >  OpenCL clCreateProgramFromSource() segfaults with address of single string #included
OpenCL clCreateProgramFromSource() segfaults with address of single string #included

Time:12-02

Calling clCreateProgramFromSource on a single source with the strings arg set the the address of an #included character array segfaults, but passing the array in as an element of a character pointer array works.

#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" will print and then the program segfaults, i'm not sure why it doesn't work with the second version, prog_src_cl is an unsigned char * in the file #included.

Any help would be much appreciated as i cant seem to wrap my head around why something so seemingly trivial doesnt work as expected.

CodePudding user response:

The function clCreateProgramWithSource expects the source to be passed via the parameter const char** strings. You say prog_src_cl is a character array. This means that &proc_src_cl is of type const char (*)[N]. (Pointer to N-element array of constant characters, where N is probably the length of the string used to initialise the array, plus 1 for the nul termination character.) This is not the same as const char**, and you cannot safely convert between them!

I recommend you turn on more type checking options in your C compiler, for example by passing -Wextra to clang or gcc. For example, clang catches this programming error in the -Wincompatible-pointer-types warning, which is included in -Wextra.

  • Related