Hello Guest

How to use PointerBuffer for array of String?

  • 3 Replies
  • 5749 Views
How to use PointerBuffer for array of String?
« on: September 30, 2021, 12:13:17 »
Hello. I need to use CL12.clCompileProgram and I need to pass additional headers as String[]. How to do this with the PointerBuffer? Is this correct what I have?
I'm not sure if the size is correct because of the null-termination. Or is there a simpler way?

Code: [Select]
    private static PointerBuffer createBuffer(String[] list) {
        var size = 0;
        for (int i = 0; i < list.length; i++) {
            size += list[i].length();
        }
        var p = PointerBuffer.allocateDirect(size);
        p.rewind();
        for (String s : list) {
            var buffer = ByteBuffer.wrap(s.getBytes());
            p.put(buffer);
        }
        p.flip();
        return p;
    }


Re: How to use PointerBuffer for array of String?
« Reply #1 on: October 01, 2021, 15:36:54 »
I tried like this bellow. But the clCompileProgram still only sees the very first header name. Is this correct to pass a String[] to a char** parameter?

Code: [Select]
    private static PointerBuffer createBuffer(String[] list) {
        var p = PointerBuffer.allocateDirect(list.length);
        p.rewind();
        for (String s : list) {
            var bytes = s.getBytes();
            var buffer = BufferUtils.createByteBuffer(s.length());
            buffer.rewind();
            buffer.put(bytes);
            buffer.flip();
            p.put(MemoryUtil.memAddress(buffer));
        }
        p.flip();
        return p;
    }


Re: How to use PointerBuffer for array of String?
« Reply #2 on: October 12, 2021, 08:35:52 »
Sorry if I'm being annoying but could I please get an answer?

Re: How to use PointerBuffer for array of String?
« Reply #3 on: November 05, 2021, 00:25:03 »
It looks like:

1. You're not null-terminating the bytes from s.getBytes()

2. You're not retaining references to the ByteBuffers allocated via BufferUtils.createByteBuffer(s.length()), and so the GC might reclaim them and free the associated native memory.