Skip to content

Instantly share code, notes, and snippets.

@jamesandersen
Created June 30, 2017 23:05
Show Gist options
  • Save jamesandersen/cb2d5754f5eee5c8edfb5ea139a184b2 to your computer and use it in GitHub Desktop.
Save jamesandersen/cb2d5754f5eee5c8edfb5ea139a184b2 to your computer and use it in GitHub Desktop.
Allocating C memory when invoking C code via CGO
// allocate 81 char string in C memory
parsed := C.CString(strings.Repeat("0", 81))
defer C.free(unsafe.Pointer(parsed)) // free it when we're done here
// allocate byte array for our image data in C memory
p := C.CBytes(data)
defer C.free(unsafe.Pointer(p)) // free it when we're done
// float32 is standardized type compatible with C
gridCoords := []float32{-1, -1, -1, -1, -1, -1, -1, -1}
// C.ParseSudoku will write to the allocated memory
C.ParseSudoku((*C.char)(p), C.int(len(data)), (*C.float)(unsafe.Pointer(&gridCoords[0])), true, parsed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment