FAQ
Hi guys,

I'm new to the Go lang, I need to use C library to work with binary data.
I'm creating [][]byte in Go and passing it as **char into C code:


package main

/*
#include "cauchy.c"
#include "galois.c"
#include "jerasure.c"
#include "liberation.c"
#include "reed_sol.c"
*/
import "C"

import "unsafe"
import "fmt"



func main() {
const k = 1;
const m = 1;
const w = 8;
k_c := C.int(k);
m_c := C.int(m);
w_c := C.int(w);
matrix := C.reed_sol_vandermonde_coding_matrix(k_c, m_c, w_c);
fmt.Printf("matrix:\n%t\n\n", matrix);

const blockLength = 1024;
  var data [k][blockLength]byte;
var coding [m][blockLength]byte;
fmt.Printf("data:\n%t\n\n", data);
fmt.Printf("coding:\n%t\n\n", coding);

data_c := (**C.char) (unsafe.Pointer(&data[0]));
coding_c := (**C.char) (unsafe.Pointer(&coding[0]));
fmt.Printf("data_c:\n%t\n\n", data_c);
fmt.Printf("coding_c:\n%t\n\n", coding_c);

C.jerasure_matrix_encode(k_c, m_c, w_c, matrix, data_c, coding_c,
C.int(blockLength));

}


The C functions called here are defined as

int *reed_sol_vandermonde_coding_matrix(int k, int m, int w);
void jerasure_matrix_encode(int k, int m, int w, int *matrix, char
**data_ptrs, char **coding_ptrs, int size)

I've tested it to work with the input values for k, m, w as 1, 1, 8


*However the app crashes on the last line // *C.jerasure_matrix_encode(k_c,
m_c, w_c, matrix, data_c, coding_c, C.int(blockLength)); *//:*

Console output:
matrix:
%!t(*main._Ctype_int=0x203950)

data:
[%!t([1024]uint8=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0])]

data_c:
%!t(**main._Ctype_char=0xc20003c400)

coding_c:
%!t(**main._Ctype_char=0xc20003c800)

SIGSEGV: segmentation violation
PC=0x7fff9190eb96
signal arrived during cgo execution

main._Cfunc_jerasure_matrix_encode(0x100000001, 0x8, 0x203950,
0xc20003c400, 0xc20003c800, ...)
command-line-arguments/_obj/_cgo_defun.c:43 +0x2f
main.main()
command-line-arguments/_obj/jerasure.cgo1.go:41 +0x447

goroutine 2 [syscall]:

goroutine 3 [runnable]:
rax 0x0
rbx 0x0
rcx 0x400
rdx 0x0
rdi 0x0
rsi 0x0
rbp 0x7fff5fbffb20
rsp 0x7fff5fbffb20
r8 0x0
r9 0xc20003c400
r10 0x0
r11 0x0
r12 0x0
r13 0xc20003c400
r14 0x1
r15 0x400
rip 0x7fff9190eb96
rflags 0x10246
cs 0x2b
fs 0x0
gs 0x0


Perhaps someone can give me a hint where to dig such a problem? How to
convert between pointers to multidimensional arrays in Go and C properly?


Thanks

--
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/groups/opt_out.

Search Discussions

  • Nick Craig-Wood at Jun 29, 2013 at 7:20 am

    On 28/06/13 16:49, vgorin@istreamdata.com wrote:
    I'm new to the Go lang, I need to use C library to work with binary
    data. I'm creating [][]byte in Go and passing it as **char into C code:


    package main

    /*
    #include "cauchy.c"
    #include "galois.c"
    #include "jerasure.c"
    #include "liberation.c"
    #include "reed_sol.c"
    */
    import "C"

    import "unsafe"
    import "fmt"



    func main() {
    const k = 1;
    const m = 1;
    const w = 8;
    k_c := C.int(k);
    m_c := C.int(m);
    w_c := C.int(w);
    matrix := C.reed_sol_vandermonde_coding_matrix(k_c, m_c, w_c);
    fmt.Printf("matrix:\n%t\n\n", matrix);

    const blockLength = 1024;
    var data [k][blockLength]byte;
    var coding [m][blockLength]byte;
    fmt.Printf("data:\n%t\n\n", data);
    fmt.Printf("coding:\n%t\n\n", coding);

    data_c := (**C.char) (unsafe.Pointer(&data[0]));
    coding_c := (**C.char) (unsafe.Pointer(&coding[0]));
    fmt.Printf("data_c:\n%t\n\n", data_c);
    fmt.Printf("coding_c:\n%t\n\n", coding_c);

    C.jerasure_matrix_encode(k_c, m_c, w_c, matrix, data_c, coding_c,
    C.int(blockLength));

    }


    The C functions called here are defined as

    int *reed_sol_vandermonde_coding_matrix(int k, int m, int w);
    void jerasure_matrix_encode(int k, int m, int w, int *matrix, char
    **data_ptrs, char **coding_ptrs, int size)
    The problem is, is that a char **pointer should be a pointer to an
    array of pointers, each one of those pointing to a row in your matrix.
    You've supplied one pointer to a pointer the first row of the matrix.

    I think you want to do something like this

         var data [k][blockLength]byte
         var data_pointers [k]*byte
         for i := range data_pointers {
             data_pointers[k] = &data[k][0]
         }
         data_c := (**C.char) (unsafe.Pointer(&data_pointers[0]));

    Same for coding_c

    Is this a publicly available library you are using? Error correction
    over GF(256) used to be one of my specialist subjects!
    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    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/groups/opt_out.
  • Vgorin at Jun 29, 2013 at 9:16 am
    Thanks, Nick, that was really helpful.

    I'm using open source lib for erasure coding by James
    Plank, http://web.eecs.utk.edu/~plank/plank/papers/CS-08-627.html
    On Saturday, June 29, 2013 10:20:26 AM UTC+3, Nick Craig-Wood wrote:
    On 28/06/13 16:49, vgo...@istreamdata.com <javascript:> wrote:
    I'm new to the Go lang, I need to use C library to work with binary
    data. I'm creating [][]byte in Go and passing it as **char into C code:


    package main

    /*
    #include "cauchy.c"
    #include "galois.c"
    #include "jerasure.c"
    #include "liberation.c"
    #include "reed_sol.c"
    */
    import "C"

    import "unsafe"
    import "fmt"



    func main() {
    const k = 1;
    const m = 1;
    const w = 8;
    k_c := C.int(k);
    m_c := C.int(m);
    w_c := C.int(w);
    matrix := C.reed_sol_vandermonde_coding_matrix(k_c, m_c, w_c);
    fmt.Printf("matrix:\n%t\n\n", matrix);

    const blockLength = 1024;
    var data [k][blockLength]byte;
    var coding [m][blockLength]byte;
    fmt.Printf("data:\n%t\n\n", data);
    fmt.Printf("coding:\n%t\n\n", coding);

    data_c := (**C.char) (unsafe.Pointer(&data[0]));
    coding_c := (**C.char) (unsafe.Pointer(&coding[0]));
    fmt.Printf("data_c:\n%t\n\n", data_c);
    fmt.Printf("coding_c:\n%t\n\n", coding_c);

    C.jerasure_matrix_encode(k_c, m_c, w_c, matrix, data_c, coding_c,
    C.int(blockLength));

    }


    The C functions called here are defined as

    int *reed_sol_vandermonde_coding_matrix(int k, int m, int w);
    void jerasure_matrix_encode(int k, int m, int w, int *matrix, char
    **data_ptrs, char **coding_ptrs, int size)
    The problem is, is that a char **pointer should be a pointer to an
    array of pointers, each one of those pointing to a row in your matrix.
    You've supplied one pointer to a pointer the first row of the matrix.

    I think you want to do something like this

    var data [k][blockLength]byte
    var data_pointers [k]*byte
    for i := range data_pointers {
    data_pointers[k] = &data[k][0]
    }
    data_c := (**C.char) (unsafe.Pointer(&data_pointers[0]));

    Same for coding_c

    Is this a publicly available library you are using? Error correction
    over GF(256) used to be one of my specialist subjects!
    --
    Nick Craig-Wood <ni...@craig-wood.com <javascript:>> --
    http://www.craig-wood.com/nick
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 28, '13 at 6:47p
activeJun 29, '13 at 9:16a
posts3
users2
websitegolang.org

2 users in discussion

Vgorin: 2 posts Nick Craig-Wood: 1 post

People

Translate

site design / logo © 2021 Grokbase