<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns="http://www.w3.org/TR/REC-html40" xmlns:o = 
"urn:schemas-microsoft-com:office:office" xmlns:w = 
"urn:schemas-microsoft-com:office:word"><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1528" name=GENERATOR>
<STYLE>@page Section1 {size: 595.3pt 841.9pt; margin: 70.85pt 70.85pt 70.85pt 70.85pt; }
P.MsoNormal {
        FONT-SIZE: 12pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"
}
LI.MsoNormal {
        FONT-SIZE: 12pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"
}
DIV.MsoNormal {
        FONT-SIZE: 12pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"
}
A:link {
        COLOR: blue; TEXT-DECORATION: underline
}
SPAN.MsoHyperlink {
        COLOR: blue; TEXT-DECORATION: underline
}
A:visited {
        COLOR: purple; TEXT-DECORATION: underline
}
SPAN.MsoHyperlinkFollowed {
        COLOR: purple; TEXT-DECORATION: underline
}
SPAN.EmailStyle17 {
        COLOR: windowtext; FONT-FAMILY: Arial; mso-style-type: personal-compose
}
DIV.Section1 {
        page: Section1
}
</STYLE>
</HEAD>
<BODY lang=BG vLink=purple link=blue>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>In the 
example you give they share memory.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>Record 
= #some_record()</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>Creates a record - this is actually stored on the 
heap.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>The 
local variable Record or the arguments to the functions are 
just</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>tagged 
pointers.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>Virtually all objects in Erlang are represented by tagged pointers 
which fit into 32 bits</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>(on a 
32 bit machine) - exceptions are things like small integers, and the 
 empty list.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>If you 
say</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006>    <FONT face=Arial 
color=#0000ff size=2>X = {1,2,3}</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006>    <FONT face=Arial 
color=#0000ff size=2>foo(X).</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>Then</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006>    <FONT face=Arial 
color=#0000ff size=2>foo(X) -></FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>        Y = g(X),</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006>   <FONT face=Arial 
color=#0000ff size=2>     ...</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>Thing 
of X = {1,2,3} as doing the following:</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>First 
malloc space for a {1,2,3} structure on the heap.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>Then 
create the {1,2,3} structure. Put a tagged pointer to</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>the 
malloced area on the heap into X.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>X is 
(internally) a 32 bit tagged pointer. This pointer is passed into foo 
and</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>so 
on.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>Heap 
data structures are garbage collected when they can no longer 
be</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>referred to. So after the call to g(X) the space used by X can be 
reclaimed,</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff size=2>if 
there are no other references to X.</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>/Joe</FONT></SPAN></DIV>
<DIV><SPAN class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=909595613-04072006>    </SPAN></DIV>
<DIV><SPAN class=909595613-04072006>     </SPAN><SPAN 
class=909595613-04072006><FONT face=Arial color=#0000ff 
size=2>  </FONT></SPAN></DIV><BR>
<BLOCKQUOTE dir=ltr 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
  <HR tabIndex=-1>
  <FONT face=Tahoma size=2><B>From:</B> owner-erlang-questions@erlang.org 
  [mailto:owner-erlang-questions@erlang.org] <B>On Behalf Of </B>Yani 
  Dzhurov<BR><B>Sent:</B> den 4 juli 2006 15:43<BR><B>To:</B> 
  erlang-questions@erlang.org<BR><B>Subject:</B> are function parameters 
  copied?<BR></FONT><BR></DIV>
  <DIV></DIV>
  <DIV class=Section1>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Hi,<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">I’ve wondered whether functions 
  share memory or for a function call new memory is allocated and parameters are 
  copied ?<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">This is what I 
  mean:<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">fun()-><o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  Record = #some_record{},<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  fun1(Record).<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">fun1(Record)-><o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  fun2(Record).<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">fun2(Record)-><o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  fun3(Record).<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">.fun3(Record)-><o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  fun4(Record).<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">fun4(Record)-><o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">            
  Some operations with records….<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Is the VM going to copy Record 
  four times for each function call or all functions will share this object 
  Record…having in mind that it’s immutable this shouldn’t be a 
  problem?<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Thanks,<o:p></o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT></P>
  <P class=MsoNormal><FONT face=Arial size=2><SPAN lang=EN-US 
  style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Jani<o:p></o:p></SPAN></FONT></P></DIV></BLOCKQUOTE></BODY></HTML>