I'm using Erlang's NIF, and the result of the C function is an array that I want to send back to erlang in the form of a list of tuples of three points, each is a tuple of two doubles.
to create this array currently I'm doing this:
ans = (ERL_NIF_TERM *)malloc(6*ntri*sizeof(ERL_NIF_TERM));
for (i=0;i<ntri;i++) {
ans[i] = enif_make_tuple3(env,
enif_make_tuple2(env,enif_make_double(env,x1[i]),enif_make_double(env,y1[i])),
enif_make_tuple2(env,enif_make_double(env,x2[i]),enif_make_double(env,y2[i])),
enif_make_tuple2(env,enif_make_double(env,x3[i]),enif_make_double(env,y3[i]))
);
}
So far, it seems to work. but is it correct?
My reasoning was that on every cell of the array ans
I have 6 doubles each with a size of ERL_NIF_TERM
, so I allocate according to this.
But is it true?
Should I count the tuples?
What is the size of ERL_NIF_TERM
anyway?
Would double inside ERL_NIF_TERM
be the same size of an int inside ERL_NIF_TERM
? a tuple of 2 ints is also an ERL_NIF_TERM
, is it the same size?