Learn to create a NIF. Receive two string inside my NIF. Please review.

Mikael Karlsson karlsson.rm@REDACTED
Tue Mar 31 22:48:45 CEST 2020


Hi,
try and change &A and &B to just A and B. Like: enif_get_string(env, argv[0],
A, 50, ERL_NIF_LATIN1);


Den tis 31 mars 2020 kl 20:08 skrev I Gusti Ngurah Oka Prinarjaya <
okaprinarjaya@REDACTED>:

> Hi,
>
> I learn to create a NIF. Inside my NIF, i need to receive two value, each
> value is from paramA, and paramB.
>
> type of value that i want to receive is string value. I need your help to
> review my code
> i afraid my code will crashing the VM.
>
> My NIF successfully, and i get the expected result.
>
> this is my code. please review.
>
> // test_nif.c
>
> static ERL_NIF_TERM lcs(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv
> [])
> {
> int ret;
> char A[50];
> char B[50];
> enif_get_string(env, argv[0], &A, 50, ERL_NIF_LATIN1);
> enif_get_string(env, argv[1], &B, 50, ERL_NIF_LATIN1);
> uint32_t lenA = strlen(A);
> uint32_t lenB = strlen(B);
>
> ret = llcs_utf8_int(A, B, lenA, lenB);
> return enif_make_int(env, ret);
> }
>
> static ErlNifFunc nif_funcs[] = {
> {"lcs", 2, lcs}
> };
>
> ERL_NIF_INIT(test_nif, nif_funcs, NULL, NULL, NULL, NULL)
>
> //
>
> test_nif:lcs("Hello World", "Hellaoo World").
>
> Thank you
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200331/98018983/attachment.htm>


More information about the erlang-questions mailing list