Re: [go-nuts] Re: How to pass Go []byte (arrays/slices) as C *char to C functions

2018-01-24 Thread 'Bryan C. Mills' via golang-nuts
On Wed, Jan 24, 2018 at 3:00 PM, Christian LeMoussel wrote: > Thank you Bryan for your advice. > In examples, I see how to pass the Go pointers directly. > > For example : > h := C.CString(name) > defer C.free(unsafe.Pointer(h)) > gerrno, err := C.getaddrinfo(h, nil, &hints, &res) > T

[go-nuts] Re: How to pass Go []byte (arrays/slices) as C *char to C functions

2018-01-24 Thread Christian LeMoussel
Thank you Bryan for your advice. In examples, I see how to pass the Go pointers directly. For example : h := C.CString(name) defer C.free(unsafe.Pointer(h)) gerrno, err := C.getaddrinfo(h, nil, &hints, &res) To h := make([]byte, len(name)+1) copy(h, name) gerrno, err := C

Re: [go-nuts] Re: How to pass Go []byte (arrays/slices) as C *char to C functions

2018-01-24 Thread 'Bryan Mills' via golang-nuts
It may be considerably more efficient to pass the Go pointers directly. To do so, you may need to convert through unsafe.Pointer. Note that many C APIs that take a char* expect it to be null-terminated, so if you take that approach you may need to append an extra 0 to the slice. https://golang.o

Re: [go-nuts] Re: How to pass Go []byte (arrays/slices) as C *char to C functions

2018-01-23 Thread Jan Mercl
On Tue, Jan 23, 2018 at 5:02 PM Christian LeMoussel wrote: > Is it correct /best way ? Hopefully/only. But as noted previously, not tested. > There is no memory leaks ? Something must eventually C.free() the memory allocated by C.CString(). -- -j -- You received this message because you

[go-nuts] Re: How to pass Go []byte (arrays/slices) as C *char to C functions

2018-01-23 Thread Christian LeMoussel
I do this devidx := C.scan_bus((*C.char)(unsafe.Pointer(&sbuf[0])), C.int(cap(sbuf)), C.CString(sn), C.CString(product)) Is it correct /best way ? There is no memory leaks ? . -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsu