After I added the unsafe.Pointer conversion the code compiled: 
https://play.golang.org/p/QTPyhZzKZH

So my understanding is that line 15 is jut a pointer type conversion to 
slice that is backed by the C array, so it doesn't matter if I choose 1 << 
20 entries or 1 << 22 entries, in both cases the runtime is just creating a 
slice?

Another question, which of the two approaches (pointer arithmetic vs 
iterating over sliced back by C array) is more efficient to create a Go 
slice?  This code will be invoked hundreds of times per minute to convert 
C arrays coming from C functions into memory safe Go slices that will then 
be used throughout the Go program.

On Wednesday, August 31, 2016 at 5:26:27 PM UTC-5, Ian Lance Taylor wrote:

> On Wed, Aug 31, 2016 at 2:06 PM, Luke Mauldin <lukem...@gmail.com 
> <javascript:>> wrote: 
> > 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 
>
> Oh, sorry, you need to drop in another conversion to unsafe.Pointer. 
>
> > 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? 
>
> There is no penalty.  It's just a pointer type conversion.  Though if 
> you are seeing that many entries, then I suspect that you left out the 
> operation that slices the pointer back down to arrLength (the 
> [:arrLength:arrLength] at the end of line in the code I sent). 
>
> 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