C.malloc of Go type?

2,216 views
Skip to first unread message

Max

unread,
May 19, 2018, 4:42:12 PM5/19/18
to golang-nuts
After reading https://golang.org/cmd/cgo/#hdr-Passing_pointers and https://blog.golang.org/c-go-cgo,
I am trying to understand if there are valid ways to pass between Go and C complicated data structures
with (potentially) lots of pointers.

At the moment this is just a speculative question, but it could quickly become useful both for my Go interpreter gomacro
and for other similar tasks (I am thinking at least about https://github.com/gijit/gi) that need to pass arbitrary
data structures between compiled Go code and the interpreter itself (which may not be written in Go).

The question is: is it allowed to take the unsafe.Pointer returned by C.malloc() and convert it to a pointer to a Go struct type?

In practice: is the following code valid ?

Thanks,

Max

// -----------------------------------------------------------------------------------------

package main

/*
#include <string.h>
*/
import "C"

import (
    "fmt"
    "unsafe"
)

type List struct {
    Node int
    Next *List
}

func mallocList() *List {
    const n = unsafe.Sizeof(List{})
    p := C.malloc(C.ulong(n))
    C.memset(p, C.int(n), 0)
    return (*List)(p)
}

func main() {
    // if this code is valid, x can contain C pointers and can be passed freely to C code,
    // thus exchanging complicated data structured between Go and C

    x := mallocList()
    x.Next = mallocList()

    fmt.Printf("%#v\n", x)
    fmt.Printf("%#v\n", x.Next)
}

Max

unread,
May 19, 2018, 4:48:08 PM5/19/18
to golang-nuts
On Saturday, May 19, 2018 at 10:42:12 PM UTC+2, Max wrote:

func mallocList() *List {
    const n = unsafe.Sizeof(List{})
    p := C.malloc(C.ulong(n))
    C.memset(p, C.int(n), 0)
    return (*List)(p)
}

I swapped the arguments to C.memset(). The correct version is:

func mallocList() *List {
    const n = C.ulong(unsafe.Sizeof(List{}))
    p := C.malloc(n)
    C.memset(p, 0, n)
    return (*List)(p)
}
 

Jan Mercl

unread,
May 19, 2018, 5:03:47 PM5/19/18
to Max, golang-nuts

On Sat, May 19, 2018 at 10:42 PM Max <massimilia...@gmail.com> wrote:

> The question is: is it allowed to take the unsafe.Pointer returned by C.malloc() and convert it to a pointer to a Go struct type?

--

-j

andrey mirtchovski

unread,
May 19, 2018, 8:10:14 PM5/19/18
to Jan Mercl, Max, golang-nuts
also useful if you're dealing with blobs of C memory:


On Sat, May 19, 2018 at 3:03 PM, Jan Mercl <0xj...@gmail.com> wrote:

On Sat, May 19, 2018 at 10:42 PM Max <massimiliano.ghilardi@gmail.com> wrote:

> The question is: is it allowed to take the unsafe.Pointer returned by C.malloc() and convert it to a pointer to a Go struct type?

See https://golang.org/pkg/unsafe/#Pointer
--

-j

--
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.

Reply all
Reply to author
Forward
0 new messages