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

郎咸武 langxianzhe@REDACTED
Wed Dec 14 14:11:17 CET 2011


Thanks Gleb Peregud.

Your are right. But a new request is come on. I input list [1,7,0,9] as
parameter. Ii only return [1,7]. The [0,9] is lose. Maybe the '0' elment as
a NUL in C.
What  should i do .

Thanks

8>complex6:get_name([1,7,0,9]).
[1,7]
9>

2011/12/14 Gleb Peregud <gleber.p@REDACTED>

> 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);
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111214/f7ffdc65/attachment.htm>


More information about the erlang-questions mailing list