# Ccall issue - totally stumped

**URL:** <https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842>\
**Category:** General Usage\
**Created:** [February 2, 2017, 1:19pm UTC](https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842 "2017-02-02T13:19:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [February 2, 2017, 1:19pm UTC](https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842/1 "2017-02-02T13:19:06Z")

</div>

I’m calling c++ code - to do this I added a C wrapper. One of the arguments (extrinsic) is an Array, which is passed as a Ptr. When the C code gets the pointer, it points to nonexistent memory. Other arguments are passed fine! I’m stumped.

Relevant code is:

```julia
type LDPC_DECODER
    s::simcase
    bofc::Array{Int32,2}
    w::Array{Int32,2}
    apost::Array{Int32,1}
    extrinsic::Array{Int32,1}
    dec_codeword::Array{Int32,1}
    LDPC_DECODER(s::simcase, bofc::Array{Int32,2}) = new(s, bofc, Array(Int32, s.CUMCHE2, s.DC2), Array(Int32,s.N), Array(Int32,s.NXMIT), Array(Int32,s.N))
end

function compute_scalar(dec::LDPC_DECODER, inp::Array{Int32,1}, xmit::Array{Int32,1}, current_it::Int, genie::Bool=true, max_it::Int=1)
    assert(length(inp) == dec.s.NXMIT && length(xmit) == dec.s.KXMIT)
    msg = Array(Int32,dec.s.K)
    msg_shortened = Array(Int32, dec.s.KXMIT)
    ccall((:ldpc_decoder_c, "./ldpc_decoder.so"), Void, (simcase_c, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Int32, Int32, Int32), dec.s, inp, dec.apost, dec.w, dec.bofc, msg, xmit, dec.extrinsic, dec.dec_codeword, Int32(genie), current_it, max_it)

```

C wrapper is:

```julia
extern "C" {
  void ldpc_decoder_c(Csimcase c,
                 int* in_c,
                 int* apost_c,
                 int* w_c,
                 int* b_c,
                 int* msg_c,
                 int* xmit_c,
                 int* extrinsic_c,
                 int* dec_codeword_c,
                 int genie,
                 int it,
                 int max_it) {

```

gdb says:

```julia
Thread 1 "julia" hit Breakpoint 1, ldpc_decoder_c (c=..., in_c=0x24747c0, apost_c=0x25c2b00, w_c=0x25bdb80, b_c=0x25abf00, 
    msg_c=0x268e7c0, xmit_c=0x253e5c0, extrinsic_c=0x7ffd00000001, dec_codeword_c=0x25de880, genie=38282368, it=1, max_it=0)
    at ldpc_decoder.cc:561

```

The args up to xmit\_c look OK, but extrinsic\_c is not a valid pointer. Also, the argument genie is wrong and current\_it and max\_it seem swapped

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 2, 2017, 1:35pm UTC](https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842/2 "2017-02-02T13:35:16Z")

</div>

Platform and version?

---

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [February 2, 2017, 1:38pm UTC](https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842/3 "2017-02-02T13:38:14Z")

</div>

This is rpm -q julia  
julia-0.5.0-1.fc25.x86\_64  
fedora linux 25

Also, here is the top-level julia code excerpt:

```julia
dec = LDPC_DECODER(s, b)
xbits, extrinsic = compute_scalar(dec, y2*2-1, x, 0, true, 20)

```

---

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [February 2, 2017, 3:48pm UTC](https://discourse.julialang.org/t/ccall-issue-totally-stumped/1842/4 "2017-02-02T15:48:54Z")

</div>

OK, I finally found my problem!

The C declaration of the simcase\_c struture wasn’t correct, it included a c+±ism

static const int blah = 1;

at the end, which had been copied/pasted from it’s c++ brother.
