Problem on trying to use a C struct in go

1,287 views
Skip to first unread message

Robert Sandra

unread,
Sep 26, 2012, 5:00:57 PM9/26/12
to golan...@googlegroups.com
Hi all,

I just start to learn cog and I am trying to use a C struct in Go. I write some simple codes as follows, when I try to compile them by using "go build -o test testc.go", the following error keeps coming out. I have no idea where the problem is. Can anyone give some hint? Thanks.

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


Matt Kane's Brain

unread,
Sep 26, 2012, 5:12:14 PM9/26/12
to Robert Sandra, golan...@googlegroups.com
sizeof is an operator, not a function. I don't think you can use
directly like that in go. You can use
C.size_t(unsafe.Sizeof(C.struct_student{}))
> --
>
>



--
matt kane's brain
http://hydrogenproject.com

Robert Sandra

unread,
Sep 26, 2012, 5:22:07 PM9/26/12
to golan...@googlegroups.com, Robert Sandra
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?

Thanks.

Matt Kane's Brain

unread,
Sep 26, 2012, 5:29:49 PM9/26/12
to Robert Sandra, golan...@googlegroups.com
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?

Robert Sandra

unread,
Sep 26, 2012, 5:38:51 PM9/26/12
to golan...@googlegroups.com, Robert Sandra
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?

//export
type Stu struct {

Kyle Lemons

unread,
Sep 26, 2012, 6:18:42 PM9/26/12
to Robert Sandra, golan...@googlegroups.com
On Wed, Sep 26, 2012 at 2:38 PM, Robert Sandra <robert.s...@gmail.com> wrote:
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?
Not that I know of.  Usually it wouldn't make sense, since the point of cgo is interoperability with existing code, and that would imply that you're writing C to interoperate with existing Go...
 
//export
type 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

--
 
 

Shane Hansen

unread,
Sep 26, 2012, 7:41:40 PM9/26/12
to Kyle Lemons, Robert Sandra, golan...@googlegroups.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{}

--
 
 

Robert Sandra

unread,
Sep 26, 2012, 9:02:44 PM9/26/12
to golan...@googlegroups.com, Kyle Lemons, Robert Sandra
are we able to do this?

suppose the size of obj a (struct student type) is 8, and i want to add two ints as the head of a, suppose the size of one int is 4, so the total size of a plus two int is 16. Can we get a memory space of length 16 by using C.malloc, that returns to us pointer a, we can do a[0] = 1, a[1] = 1, a[2] is the pointer to the object of struct student type.

Is it possible?

Thanks.

minux

unread,
Sep 27, 2012, 4:12:29 AM9/27/12
to Shane Hansen, Kyle Lemons, Robert Sandra, golan...@googlegroups.com
On Thu, Sep 27, 2012 at 7:41 AM, Shane Hansen <shanem...@gmail.com> wrote:
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{}
The problem with this approach is that if the C code retain this pointer, Go's GC won't
see it, and might garbage collect it.

Of course, if you're sure C code won't keep the pointer, this approach works fine.

Shane Hansen

unread,
Sep 27, 2012, 11:25:09 AM9/27/12
to minux, Kyle Lemons, Robert Sandra, golan...@googlegroups.com
I may be missing the point, so I apologize if I'm being dense. But I think the cleanest way to do this is:

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

(My example code was getting a little long). It's possible to use malloc and cast to a go pointer, and unsafe.Sizeof can help determine the size of a structure. But by far the easiest way to do this imo is
by letting go allocate all the structures, c or otherwise.
Reply all
Reply to author
Forward
0 new messages