Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass struct and array of structs to C function from Go

Tags:

go

cgo

Stuck with this problem. Able to get only the first member of passed structure... What I do wrong? And what is the right way to pass the structure from Go to C?

This is my example of how it doesn't work:

package main

/*
#include <stdio.h>

typedef struct {
    int a;
    int b;
} Foo;

void pass_array(Foo **in) {
    int i;

    for(i = 0; i < 2; i++) {
        fprintf(stderr, "[%d, %d]", in[i]->a, in[i]->b);
    }
    fprintf(stderr, "\n");
}

void pass_struct(Foo *in) {
    fprintf(stderr, "[%d, %d]\n", in->a, in->b);
}

*/
import "C"

import (
    "unsafe"
)

type Foo struct {
    A int
    B int
}

func main() {

    foo := Foo{25, 26}
    foos := []Foo{{25, 26}, {50, 51}}

    // wrong result = [25, 0]
    C.pass_struct((*_Ctype_Foo)(unsafe.Pointer(&foo)))

    // doesn't work at all, SIGSEGV
    // C.pass_array((**_Ctype_Foo)(unsafe.Pointer(&foos[0])))

    // wrong result = [25, 0], [50, 0]
    out := make([]*_Ctype_Foo, len(foos))
    out[0] = (*_Ctype_Foo)(unsafe.Pointer(&foos[0]))
    out[1] = (*_Ctype_Foo)(unsafe.Pointer(&foos[1]))
    C.pass_array((**_Ctype_Foo)(unsafe.Pointer(&out[0])))
}
like image 915
tacobot Avatar asked Nov 11 '13 16:11

tacobot


1 Answers

I know this is a fairly old topic but I stumbled across it. Here's a modified (correct) version with some additional manipulation from C land on the Go structs.

package main

/*
#include <stdio.h>

typedef struct {
    int a;
    int b;
} Foo;

void pass_struct(Foo *in) { printf("%d : %d\n", in->a, in->b); }

void pass_array(Foo **in, int len) {
    for(int i = 0; i < len; i++) {
        pass_struct(in[i]);
        in[i]->a += 1;
        in[i]->b += 1;
    }
}
*/
import "C"

import (
    "fmt"
    "unsafe"
)

type Foo struct{ a, b int32 }

func main() {
    foo := Foo{10, 20}
    foos := []*Foo{&Foo{1, 2}, &Foo{3, 4}}

    fmt.Println("from C land")
    C.pass_struct((*C.Foo)(unsafe.Pointer(&foo)))
    C.pass_array((**C.Foo)(unsafe.Pointer(&foos[0])), C.int(len(foos)))
    fmt.Println("a & b should have incremented with 1")

    fmt.Println("from Go land")
    for _, foo := range foos {
        fmt.Printf("%d : %d\n", foo.a, foo.b)
    }
}

Output:

from C land
10 : 20
1 : 2
3 : 4
a & b should have incremented with 1
from Go land
2 : 3
4 : 5
like image 132
Jeffrey W. Avatar answered Sep 20 '22 16:09

Jeffrey W.