FAQ
I was pretty sure that for the following type conversions I would have to
go through an intermediate type conversion to unsafe.Pointer, but to my
beginner surprise this works:

var x [5]byte
C.f1( (*C.uint8_t)(&x[0]), 5, ... )
C.f2( (*C.uchar)(&x[0]), 5, ... )

Is it because C.uint8_t and C.uchar are aliases for byte? Something else?

And I assume it's safe to pass a Go byte buffer like this to a C function to directly fill it out with data, correct? I don't need to C.malloc (and later C.free) a C buffer and then copy from it or use C.GoBytes

Thanks.

Tim




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

Search Discussions

  • Kevin Malachowski at Dec 21, 2015 at 2:34 am
    There are new explicit rules about when its safe to pass memory between Go and C: https://tip.golang.org/doc/go1.6#cgo

    I'm guessing that both conversions work because both uint8_t and uchar are the same thing? I haven't done a lot of C recently, but arent all three types unsigned, 8-bit numbers?

    --
    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.
  • Kevin Malachowski at Dec 21, 2015 at 2:47 am
    Ah sorry, not sure about pointer conversions. It might be as you suggest, but I really don't know.

    --
    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.
  • Ian Lance Taylor at Dec 21, 2015 at 5:42 pm

    On Sat, Dec 19, 2015 at 4:04 PM, Tim K wrote:
    I was pretty sure that for the following type conversions I would have to go
    through an intermediate type conversion to unsafe.Pointer, but to my
    beginner surprise this works:

    var x [5]byte
    C.f1( (*C.uint8_t)(&x[0]), 5, ... )
    C.f2( (*C.uchar)(&x[0]), 5, ... )

    Is it because C.uint8_t and C.uchar are aliases for byte? Something else?

    And I assume it's safe to pass a Go byte buffer like this to a C function to
    directly fill it out with data, correct? I don't need to C.malloc (and later
    C.free) a C buffer and then copy from it or use C.GoBytes
    It's because Go permits conversions between unnamed types as long as
    the underlying types are identical. In this case the unnamed types
    are *C.uint8_t (and *C.uchar) and *byte. The underlying type of both
    C.uint8_t and C.uchar is uint8, which is also the underlying type for
    byte. So *C.uint8_t and *byte have identical underlying types, and
    the conversion is permitted.

    Ian

    --
    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.
  • Tim K at Dec 22, 2015 at 8:51 pm

    On Monday, December 21, 2015 at 9:42:30 AM UTC-8, Ian Lance Taylor wrote:
    On Sat, Dec 19, 2015 at 4:04 PM, Tim K <tim....@gmail.com <javascript:>>
    wrote:
    I was pretty sure that for the following type conversions I would have to go
    through an intermediate type conversion to unsafe.Pointer, but to my
    beginner surprise this works:

    var x [5]byte
    C.f1( (*C.uint8_t)(&x[0]), 5, ... )
    C.f2( (*C.uchar)(&x[0]), 5, ... )

    Is it because C.uint8_t and C.uchar are aliases for byte? Something else?
    And I assume it's safe to pass a Go byte buffer like this to a C
    function to
    directly fill it out with data, correct? I don't need to C.malloc (and later
    C.free) a C buffer and then copy from it or use C.GoBytes
    It's because Go permits conversions between unnamed types as long as
    the underlying types are identical. In this case the unnamed types
    are *C.uint8_t (and *C.uchar) and *byte. The underlying type of both
    C.uint8_t and C.uchar is uint8, which is also the underlying type for
    byte. So *C.uint8_t and *byte have identical underlying types, and
    the conversion is permitted.
    As usual, thank you Ian for reminding me about unnamed types.

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 20, '15 at 12:04a
activeDec 22, '15 at 8:51p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase