Questions about how lisp uses memory

Discussion of Common Lisp

Questions about how lisp uses memory

Postby mgcheung » Sun Jul 01, 2012 4:03 am

I have a few questions. If there is more than one variable with the same value, are there two places in memory with the same value or do they both point to the same address in memory? Also, is there any difference between having a constant value in a function vs getting the value from a parameter? Are the answers to these questions implementation specific at all? Thanks.
mgcheung
 
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Re: Questions about how lisp uses memory

Postby sinnatagg » Sun Jul 01, 2012 4:33 am

mgcheung wrote:I have a few questions. If there is more than one variable with the same value, are there two places in memory with the same value or do they both point to the same address in memory?


It is my understanding that this is often the case for numbers and characters, but that you can't rely on it. See the hyperspec on eq() and eql().

http://www.lispworks.com/documentation/HyperSpec/Body/f_eql.htm#eql
http://www.lispworks.com/documentation/HyperSpec/Body/f_eq.htm

-a
sinnatagg
 
Posts: 28
Joined: Tue Apr 21, 2009 3:04 am

Re: Questions about how lisp uses memory

Postby Kompottkin » Sun Jul 01, 2012 8:34 am

mgcheung wrote:If there is more than one variable with the same value, are there two places in memory with the same value or do they both point to the same address in memory?


It depends on what you mean by “same.” When you have an object with identity, such as an array, a list, a struct, a CLOS class instance, etc., the object will never be copied unless you do so explicitly. That is, two variables “containing” the same object do not actually both contain an instance of it. Instead, they each contain a descriptor that points to the same region in memory.

Some objects have a weaker notion of identity. Many integers, for example, are so small as to fit into a descriptor. These are called FIXNUMs. Since there is no point in wasting memory by allocating a cell on the heap when the FIXNUM takes just as much space as a pointer, they are usually stored directly inside the descriptor. There are other values that are usually handled in this way (such as characters and, sometimes, the value NIL). The general term for such an object is immediate value.

Also, is there any difference between having a constant value in a function vs getting the value from a parameter? Are the answers to these questions implementation specific at all? Thanks.


Yes, and yes. Implementations behave differently in this regard. In practice, constants are different from parameters in that:

  • They can be optimized away (in arithmetic expressions, for example).
  • They are constructed prior to runtime.
  • The system is allowed to put them into read-only memory or coalesce them (in case of file compilation).

The most important difference, however, is that destructive modification of constants has undefined consequences. Always remember that while (LIST 1 2 3) constructs a list at run-time that is fair game for modification, the value of '(1 2 3) is a constant and may not be destructively modified.

(Conceptually, literals are actually part of the source code of your program, so by modifying a literal, you modify program code.)
User avatar
Kompottkin
 
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 1 guest

cron