2

Can I pass *[]string to C from Go and then append to the string slice, or is it violating the pointer passing spec?

Go code may pass a Go pointer to C, provided the Go memory to which it points does not contain any Go pointers.

Example code:

package main

/*
extern void go_callback(void*, char*);

static inline void callback(void* stringSliceGoPointer) {
    go_callback(stringSliceGoPointer, "foobar");
}
*/
import "C"

import (
    "fmt"
    "unsafe"
)

func main() {
    a := make([]string, 0)
    C.callback(unsafe.Pointer(&a)) 
    fmt.Println(a[0]) // outputs foobar
}

//export go_callback
func go_callback(stringSliceGoPointer unsafe.Pointer, msg *C.char) {
    slice := (*[]string)(stringSliceGoPointer)
    *slice = append(*slice, C.GoString(msg))
}

2 Answers 2

3

No, It's not possible.

Refer this for further explanation of go data types.

Basically a string type in Go looks something like this.

str := "hello"

This is stored as,

 str:                0xad234e3b:
 ┌──────────┬─┐      ┌───┬───┬───┬───┬───┐
 |0xad234e3b|5|  ┌──>|104|101|108|108|111| -->[5]byte
 └────┬─────┴─┘  |   └───┴───┴───┴───┴───┘
      └──────────┘

Consider a slice:

arr := string{"hi!","hello"}

Further Slice data type contains pointer, length, capacity.

arr:                   0xd2b564c7:        0xad234e40:
┌──────────┬─┬─┐       ┌──────────┬─┐     ┌───┬───┬──┐
|0xd2b564c7|2|2|  ┌──> |0xad234e40|3|────>|104|105|33| -->[3]byte
└────┬─────┴─┴─┘  |    ├──────────┼─┤     └───┴───┴──┘
     └────────────┘    |0xad234e4b|5|──┐  0xad234e4b:
                       └──────────┴─┘  |  ┌───┬───┬───┬───┬───┐
                                       └─>|104|101|108|108|111| -->[5]byte
                                          └───┴───┴───┴───┴───┘

Where the hex value represents address.

Where the actual data is stored is of an array of [x]byte.

x represents the size of data(array).

It's clear []string itself contains many(x) pointers, whereas *[]string is one more additional pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Meaning []string is really just [][]byte which is a byte slice which then translates to an array? So it's a pointer to a pointer violating the spec?
Sorry, I haven't written the whole answer, It's posted by mistake, I'm still writing the answer.
2
+100

Passing a *[]string to C is not allowed because the memory pointed to contains strings and strings contain pointers. As the cgo docs say (emphasis mine)

Note that values of some Go types, other than the type's zero value, always include Go pointers. This is true of string, slice, interface, channel, map, and function types.

One way to overcome this is to refer to the []string more indirectly so only Go code actually knows its address. For example:

package main

/*
extern void go_callback(int, char*);

static inline void callback(int stringSliceRef) {
    go_callback(stringSliceRef, "foobar");
}
*/
import "C"

import (
    "fmt"
)

// If you need to use these values concurrently,
// you'll need more code to protect this.
var stringSlices = make([][]string, 10)

func main() {
    C.callback(0) 
    fmt.Println(stringSlices[0][0]) // outputs foobar
}

//export go_callback
func go_callback(ref C.int, msg *C.char) {
    i := int(ref)
    stringSlices[i] = append(stringSlices[i], C.GoString(msg))
}

1 Comment

Accepting this answer as it provides a possible solution to the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.