package main
/*
#include <stdlib.h>
#include <stdio.h>
struct student {
int age;
};
*/
import "C"
import (
"fmt"
// "unsafe"
// "reflect"
)
type Stu C.struct_student
func main () {
p := (*Stu)(C.malloc(C.sizeof(C.struct_student)))
fmt.Println(p)
}
Error:
# command-line-arguments
testc.go:12: error: expected expression before ')' token
testc.go:13: error: expected expression before '}' token
testc.go:17: error: expected expression before ',' token
I just don't know how to export a type from Go to C, since the cog doc only mentions exporting a Go function to C..Can we do export a type in the same way as we do with function?
//exporttype Stu struct {}
On Wednesday, September 26, 2012 5:30:18 PM UTC-4, mkb wrote:Try it and see. I don't think GC will collect this memory.
On Wed, Sep 26, 2012 at 5:22 PM, Robert Sandra<robert.s...@gmail.com> wrote:
> Thank you. That works.
>
> Just another question, can we define a struct type in Go and then use
> C.malloc to allocate memory to the instance of this type?
--
matt kane's brain
http://hydrogenproject.com
--
--
I respectfully suggest you're over-thinking it. Take advantage of go's awesomeness and just treat it like a normal struct.a := &C.struct_student{}