[erlang-questions] Adding binary data to a list

Richard A. O'Keefe ok@REDACTED
Mon Jan 25 00:38:37 CET 2016


On 24/01/2016, at 9:23 am, Theepan <vasdeveloper@REDACTED> wrote:
> If I add the same binary data to different lists, will it be copied? I know for a fact that binary data are referenced, but not sure in list making case.

Suppose you have this code:

   X = ...any arbitrary Erlang term...
   P = ...any list you like...
   Q = ...any other list you like...
   P1 = [X|P],
   Q1 = Q ++ [X]

X is not copied, no matter what it is, no matter how small,
no matter how big.  Making P1 copies nothing, just creates
one new pair.  Making Q1 copies the spine of Q, but not the
elements of Q, and not X.

You are probably thinking about the fact that
large binaries are *not* copied when they are
sent to another process, and that they are
reference counted for that purpose.








More information about the erlang-questions mailing list