I modified my example based on the code example you gave at the bottom for 
the pointer arithmetic and that compiled without any go vet errors.  I 
attempted to use the slice whose backing array is the C array 
https://play.golang.org/p/rJoDMu5YAQ but I get a compile error:
main.go:14: cannot convert cArr (type *Tag) to type *[1048576]Tag


Another question, if I update my code to use the slice whose backing array 
is the C array, I see the slice type supports 1048576 entries. If I only 
need at most 100 entries, is there a memory penalty imposed by declaring 
the type to support 1048576 entries?  If so, how great is that penalty?


On Wednesday, August 31, 2016 at 3:17:30 PM UTC-5, Ian Lance Taylor wrote:

> On Wed, Aug 31, 2016 at 12:47 PM, Luke Mauldin <lukem...@gmail.com 
> <javascript:>> wrote: 
> > 
> > I have questions about pointer arithmetic illustrated by this Play 
> example: 
> > https://play.golang.org/p/-cZteTY_M2 
> > 
> > Questions: 
> > 1) Is this the best way to do pointer arithmetic in Go to process a C 
> array 
> > and convert it to a Go slice? 
> > 2) Go vet gives an error on line 18 but doesn't give much information on 
> how 
> > to fix the usage of unsafe.Pointer.  Any recommendations? 
>
> It's usually simplest to write an expression like 
>     s := (*[1 << 20]Tag)(Carr)[:arrLength:arrLength] 
> That will give you a slice whose backing array is the C array, without 
> requiring any copying.  Of course you then have to make sure that C 
> array lives on the heap as least as long as the slice does.  If that 
> is an issue, then write 
>     s2 := make([]Tag, arrLength) 
>     copy(s2, s) 
> To be clear, this assume there are fewer than 1 << 20 entries in the 
> array, so adjust as needed. 
>
>
> The vet errors for your code are false positives, assuming that cArr 
> is allocated in C memory.  That said, it's easy to avoid them by 
> writing code like 
>
>     q := unsafe.Pointer(cArr) 
>     for i := 0; i < arrLength; i++ { 
>         p := (*Tag)(q) 
>         ret = append(ret, int(*p)) 
>         q = unsafe.Pointer(uintptr(q) + unsafe.Sizeof(q)) 
>     } 
>
> The point is: always keep pointers as pointers, except in expressions 
> that convert to uintptr and back in a single expression. 
>
> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to