[erlang-questions] [Erlang][Request] Invoke C funtion in Erlang

Gleb Peregud gleber.p@REDACTED
Wed Dec 14 13:37:17 CET 2011


2011/12/14 郎咸武 <langxianzhe@REDACTED>:
> hi,
> I using nif mehod to Invoke C funtion in Erlang.
> come code:
> 1,
> char *y;
> if (!enif_get_string(env, argv[0], y, sizeof(y), ERL_NIF_LATIN1))
>
> I input "123456" args, but only return "123".
>
> 2,char *y;
> if (!enif_get_string(env, argv[0], y, 10, ERL_NIF_LATIN1))
>
> this only appear Segmentation fault
>
> I have no idea.
> Please help me.
>
> Thanks.
> From jason.

"char *y" is actually just a pointer, so sizeof(y) returns 4, which is
a size of the pointer. Since you don't know how long is the string,
you should prepare a big enough buffer to receive it. So try the
following code:

#define MAXBUFLEN       1024

char y[MAXBUFLEN];
if (enif_get_string(env, argv[0], y, MAXBUFLEN, ERL_NIF_LATIN1) < 1)
    return enif_make_badarg(env);



More information about the erlang-questions mailing list