The fixed-size array approach is slightly easier and requires less
knowledge of how things work internally, but it also doesn't let you
set the slice's capacity correctly, which could result in some pretty
big problems depending on how the slice is used. It'd look something
like this:
device_ids_go = (*[1<<20]C.cl_device_id)(device_ids)[:num_devices]
The slice header approach is safer, but a little more involved:
var header reflect.SliceHeader
header.data = uintptr(device_ids)
header.len = num_devices
header.cap = num_devices
device_ids_go = *(*[]C.cl_device_id)(unsafe.Pointer(&header))
I might have gotten the conversions wrong, but the real versions
should be pretty close to those.
- Evan
if you know the type of the slice, you can just use make, and
use the address of the first element.
e.g.
devs := make([]C.cl_device_id, num_devices)
C.clGetContextInfo(c.c_context, C.CL_CONTEXT_DEVICES,
num_devices, &devs[0], &num_devices)
(make sure that num_devices > 0, though, because you can't
take the address of the first element of an empty array)