Created
June 30, 2017 23:05
-
-
Save jamesandersen/cb2d5754f5eee5c8edfb5ea139a184b2 to your computer and use it in GitHub Desktop.
Allocating C memory when invoking C code via CGO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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