I'm an Erlang enthusiast and a newbie Erlang Prorammer.
I recently had to face a data crunching problem in Erlang. Hence I decided to use NIFs. I have two list of proplists and I have to return the hash join of the two proplists based upon a term.
To implement it I decided to learn binary manipulation in C. Also I'm using the nifpp library(C++11) to ease my learning experience. Source: https://github.com/goertzenator/nifpp
The following is the code I can think of for extracting and copying binary to another ErlNifBinary which is well documented. But I'm not able to manipulate it.
ErlNifBinary ebin, bin_term;
nifpp::get_throws(env, argv[0], ebin); // Assigns ebin to the input binary
enif_alloc_binary(16, &bin_term); // Size of new binary
memcpy(bin_term.data, ebin.data, ebin.size); // Copying the contents of binary
I have a pointer to an unsigned char
in bin_term.data
. How do I change the contents in bin_term.data
?
Also, if I return the existing copied binary, I get different outputs for the same input and not equal to the input considering the fact that I have just coped the data from the input.
return enif_make_binary(env, &bin_term);
In the erlang REPL, if I execute the function with params <<7>>
or any other binary, I get the results as <<7,127,128,29,0,0,0,0,1,0,0,0,24,1,0,0>>
or some random dynamic values each time. Can someone point as to what is the mistake in the code.