Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Joe Armstrong (AL/EAB) <joe.armstrong <at> ericsson.com>
Subject: RE: are function parameters copied?
Newsgroups: gmane.comp.lang.erlang.general
Date: Tuesday 4th July 2006 14:09:18 UTC (over 11 years ago)
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: [email protected]
[mailto:[email protected]] On Behalf Of Yani Dzhurov
	Sent: den 4 juli 2006 15:43
	To: [email protected]
	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
 
CD: 2ms