cerlangerlang-nif

Creating a Erlang binary through NIF with data pointing to a char *


I'm doing a C-Erlang integration using NIF and I'm having some trouble to initialize a Binary and add a char* pointing to its data.

I'm doing this way:

ErlNifBinary *output_binary;
enif_alloc_binary(500, output_binary);

strcpy(output_binary->data, "Here is a string");
return enif_make_binary(env, output_binary); 

Any ideas about what I'm not doing right? I'm getting a segmentation fault.

UPDATE: I get rid of the segmentation fault. But now I can't return a Erlang binary containing a String.

Using enif_make_string with the binary.data I get a String on Erlang. But when I try to use enif_make_binary with the binary, I get things like this <<0,0,0,0,0,0,0,0,112,40,129,20>> what do I need to do to convert?


Solution

  • Just found out the problem.

    I was supposed to return the address of a binary not a binary so just a return enif_make_binary(env, &output_binary); make it work.