are function parameters copied?

Joe Armstrong (AL/EAB) <>
Tue Jul 4 16:09:18 CEST 2006


In the example you give they share memory.
 
Record = #some_record()
 
Creates a record - this is actually stored on the heap.
 
The local variable Record or the arguments to the functions are just
tagged pointers.
 
Virtually all objects in Erlang are represented by tagged pointers which
fit into 32 bits
(on a 32 bit machine) - exceptions are things like small integers, and
the  empty list.
 
If you say
 
    X = {1,2,3}
    foo(X).
 
Then
 
    foo(X) ->
        Y = g(X),
        ...
 
Thing of X = {1,2,3} as doing the following:
 
First malloc space for a {1,2,3} structure on the heap.
Then create the {1,2,3} structure. Put a tagged pointer to
the malloced area on the heap into X.
 
X is (internally) a 32 bit tagged pointer. This pointer is passed into
foo and
so on.
 
Heap data structures are garbage collected when they can no longer be
referred to. So after the call to g(X) the space used by X can be
reclaimed,
if there are no other references to X.
 
/Joe
 
    
       


________________________________

	From: 
[mailto:] On Behalf Of Yani Dzhurov
	Sent: den 4 juli 2006 15:43
	To: 
	Subject: are function parameters copied?
	
	

	Hi,

	 

	I've wondered whether functions share memory or for a function
call new memory is allocated and parameters are copied ?

	This is what I mean:

	fun()->

	            Record = #some_record{},

	            fun1(Record).

	 

	fun1(Record)->

	            fun2(Record).

	fun2(Record)->

	            fun3(Record).

	.fun3(Record)->

	            fun4(Record).

	fun4(Record)->

	            Some operations with records....

	 

	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?

	 

	Thanks,

	 

	Jani

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060704/86ece0d9/attachment.html>


More information about the erlang-questions mailing list