FAQ
As discussed already, it is invalid to pass go pointers to cgo.
According to cgo wiki, there is C.CString function, that make copy from go
string to char*.
(Additional question, how this function make safe copy of go string, it
already run in cgo and GC work in parallel ?)
But there is no equal function for byte slice.

The solution i came to, in addition to doubling memory allocations, also
double number of cgo calls:

package main

import (
//#include <stdlib.h>
//#include <string.h>
//int some_function( char *bytes, int num_bytes) {
// int i, sum = 0;
// for ( i = 0; i<num_bytes; i++) {
// sum += bytes[i];
// }
// return sum;
//}
"C"
"fmt"
)

func main() {
data := []byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9}
cdata := C.malloc( C.size_t(len(data)))
copy( (*[1<<24]byte)(cdata)[0:len(data)], data)
fmt.Println( C.some_function( (*C.char)(cdata), C.int(len(data))) )
}

http://play.golang.org/p/lEB1aRWelZ


Is there a better way doing this ?

Thanks,
Djadala


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 4 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 7, '15 at 8:20a
activeJan 7, '15 at 9:27a
posts4
users2
websitegolang.org

2 users in discussion

Djadala: 2 posts Minux: 2 posts

People

Translate

site design / logo © 2021 Grokbase