[erlang-questions] Accessing A Binary as an Array of Floats/Doubles Within a NIF

John Duffy jb_duffy@REDACTED
Mon Jun 20 21:04:46 CEST 2016


Hi
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.
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.
Any help would be mud appreciated.
Kind regards
John 
#include "erl_nif.h"
static ERL_NIF_TERM my_nif_1(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary Bin;
    if (!enif_inspect_binary(env, argv[0], &Bin)) {
        return enif_make_badarg(env);
    }
    // So far, so good...
    // From the erl shell...
    // my_nifs:my_nif_1(<<1.0/float>>). returns a Bin.size of 8
    // my_nifs:my_nif_1(<<1.0/float,2.0/float>>. returns a Bin.size of 16
    //
    // How do I access Bin.data as an array of doubles?
    //
    return enif_make_int(env, Bin.size); // For testing... this works.
}
static ErlNifFunc nif_funcs[] = {
    {"my_nif_1", 1, my_nif_1}
};
ERL_NIF_INIT(my_nifs, nif_funcs, NULL, NULL, NULL, NULL);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160620/f3afa3c9/attachment.htm>


More information about the erlang-questions mailing list