[erlang-questions] Question about buffer size given to erl_receive_msg in erl_connect

Jesper Louis Andersen <>
Thu Aug 31 21:08:30 CEST 2017


Hi,

In C, a buffer is represented by a pointer. Pointers do not carry size
information, and there is no means by which you know where the
end-of-buffer is. Hence, it is C-idiomatic to supply another argument,
bufsize, indicating the size of the buffer the pointer is pointing at.

If you give a too small bufsize, you are not going to use up the whole
buffer. If you give a too large bufsize, you will overwrite data after the
buffer in memory. This is wrong and usually leads to program crashes. Also,
allocation checkers will detect this for you and warn you that your program
is wrong.

If the message does not fit into the buffer, the call will fail and set and
error indicating that the message is too large to fit inside the buffer.
You could then try to extend the buffer and try again.

The xreceive call assumes the buffer is allocated by malloc. If it doesn't
fit, the call will create a buffer large enough for the message and
reallocate. This is why the types are different: you supply the addresses
of the buffer pointer and the bufsize counter. Hence the call can update
the contents of those addresses with the newly formed buffer, should it
decide to move them.

The reason both calls exist is because it isn't always the case that the
buffer will be allocated by malloc on the heap. You may statically allocate
the buffer, or allocate it on the stack, for instance.

On Thu, Aug 31, 2017 at 8:18 PM Lukas Rieder <> wrote:

> Hello everyone,
>
> I have a question about the bufsize argument in the erl_receive_msg
> function provided in erl_connect.
>
> I do not quite understand what impact a given bufsize has on the operation
> of a C-Node.
>
> Is it limiting the amount of data that can be transferred to a C-Node?
>
> Quickly trying some things out, with a buffer size of 1000 chars, I could
> send messages to the C-Node back and forth, that are way bigger than a 1000
> char buffer.
> So I suspect the buffer is only a "buffer" and not limiting anything per
> se.
>
> But what is a good bufsize?
> And when does one use erl_xreceive_msg over erl_receive_msg?
>
> I could not find any good examples on the internet on erl_xreceive_msg.
> The implementations I found that are using erl_receive_msg all are using a
> bufsize of 1000.
> I would be very happy, for my own understanding, if someone can give me
> some hints or explanation on making these choices.
>
> Kind regards,
>
>
> Lukas Rieder
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170831/012da20f/attachment.html>


More information about the erlang-questions mailing list