Hi<div><br></div><div>I'm using FreeBSD 10.3 & Erlang 18.3 and I'm trying to get to grips with passing a binary of floats to a NIF for further processing as as array of C doubles.</div><div><br></div><div>My simple NIF module compiles and I can access my "my_nif_1" function from the erlang shell. I can access the 'size' structure member but I can't figure out how to access the 'data' structure member as a C array. I understand that 'data' is a block of memory of UINT bytes but I can't figure out how to "cast" this to a C array within the NIF.</div><div><br></div><div>Any help would be mud appreciated.</div><div><br></div><div>Kind regards</div><div><br></div><div>John </div><div><br></div><div><p style="margin: 0px; font-family: Consolas; color: rgb(159, 38, 116);"><font size="2" style="background-color: rgb(255, 255, 255);"><span style="color: rgb(107, 107, 147);"><br></span></font></p><p style="margin: 0px; font-family: Consolas; color: rgb(159, 38, 116);"><font size="2" style="background-color: rgb(255, 255, 255);"><span style="color: rgb(107, 107, 147);">#include</span><span style="color: rgb(76, 47, 45);"> </span>"erl_nif.h"</font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><font size="2" style="background-color: rgb(255, 255, 255);"><br></font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);">static</span><span style="background-color: rgb(255, 255, 255);"> </span><span style="background-color: rgb(255, 255, 255); font-size: small; color: rgb(20, 142, 14);">ERL_NIF_TERM</span><span style="background-color: rgb(255, 255, 255);"> my</span><span style="background-color: rgb(255, 255, 255); color: rgb(69, 51, 255);">_nif_1</span><span style="background-color: rgb(255, 255, 255);">(</span><span style="background-color: rgb(255, 255, 255); font-size: small; color: rgb(20, 142, 14);">ErlNifEnv</span><span style="background-color: rgb(255, 255, 255);"> * </span><span style="background-color: rgb(255, 255, 255); color: rgb(182, 102, 10);">env</span><span style="background-color: rgb(255, 255, 255);">, </span><span style="background-color: rgb(255, 255, 255); font-size: small; color: rgb(20, 142, 14);">int</span><span style="background-color: rgb(255, 255, 255);"> </span><span style="background-color: rgb(255, 255, 255); color: rgb(182, 102, 10);">argc</span><span style="background-color: rgb(255, 255, 255);">, </span><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);">const</span><span style="background-color: rgb(255, 255, 255);"> </span><span style="background-color: rgb(255, 255, 255); font-size: small; color: rgb(20, 142, 14);">ERL_NIF_TERM</span><span style="background-color: rgb(255, 255, 255);"> </span><span style="background-color: rgb(255, 255, 255); color: rgb(182, 102, 10);">argv</span><span style="background-color: rgb(255, 255, 255);">[])</span></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><font size="2" style="background-color: rgb(255, 255, 255);">{</font></p>
<p style="margin: 0px; font-family: Consolas;"><font size="2" style="background-color: rgb(255, 255, 255);"><font color="#148e0e"><span>    </span>ErlNifBinary</font><span style="color: rgb(76, 47, 45);"> </span><font color="#b6660a">Bin</font><span style="color: rgb(76, 47, 45);">;</span></font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);"><br></span></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);"><span>    </span>if</span><span style="background-color: rgb(255, 255, 255); font-size: small;"> (!enif_inspect_binary(env, argv[0], &Bin)) {</span></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);"><span>    <span>    </span></span>return</span><span style="background-color: rgb(255, 255, 255); font-size: small;"> enif_make_badarg(env);</span></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><span style="background-color: rgb(255, 255, 255); font-size: small;"><span>    </span>}</span></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span style="background-color: rgb(255, 255, 255); color: rgb(185, 58, 255);"><br></span></p><p style="margin: 0px; min-height: 19px;"><span style="background-color: rgb(255, 255, 255);"><font color="#b93aff" face="Consolas">    // So far, so good...</font></span></p><p style="margin: 0px; min-height: 19px;"><span style="background-color: rgb(255, 255, 255);"><font color="#b93aff" face="Consolas">    // From the erl shell...</font></span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas;"><span>    </span>// </span><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);">my_nifs:my_nif_1(<<1.0/float>>). returns a Bin.size of 8</span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);"><span>    // my_nifs:my_nif_1(<<1.0/float,2.0/float>>. returns a Bin.size of 16</span><br></span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);"><br></span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);"><span>    //</span><br></span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);"><span>    // How do I access Bin.data as an array of doubles?</span><br></span></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span>    //</span><br></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span><br></span></p><p style="margin: 0px; min-height: 19px;"><span style="color: rgb(185, 58, 255); font-family: Consolas; background-color: rgb(255, 255, 255);"><span>    </span>return</span><span style="color: rgb(76, 47, 45); font-family: Consolas; background-color: rgb(255, 255, 255); font-size: small;"> enif_make_int(env, Bin</span><span style="background-color: rgb(255, 255, 255);"><font color="#4c2f2d" face="Consolas" size="2">.size); // For testing... this works.</font></span></p><p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><span style="background-color: rgb(255, 255, 255); font-size: small;">}</span></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><br></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(20, 142, 14);"><font size="2" style="background-color: rgb(255, 255, 255);"><span style="color: #b93aff">static</span><span style="color: #4c2f2d"> </span>ErlNifFunc<span style="color: #4c2f2d"> </span><span style="color: #b6660a">nif_funcs</span><span style="color: #4c2f2d">[] = {</span></font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><font size="2" style="background-color: rgb(255, 255, 255);"><span>    </span>{<span style="color: #9f2674">"my_nif_1"</span>, 1, my_nif_1}</font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><font size="2" style="background-color: rgb(255, 255, 255);">};</font></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45); min-height: 19px;"><br></p>
<p style="margin: 0px; font-family: Consolas; color: rgb(76, 47, 45);"><font size="2" style="background-color: rgb(255, 255, 255);">ERL_NIF_INIT(my_nifs, nif_funcs, <span style="color: #118c8d">NULL</span>, <span style="color: #118c8d">NULL</span>, <span style="color: #118c8d">NULL</span>, <span style="color: #118c8d">NULL</span>);</font></p></div><div><br></div>