Questions about how lisp uses memory
Questions about how lisp uses memory
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.
Re: Questions about how lisp uses 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().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?
http://www.lispworks.com/documentation/ ... ql.htm#eql
http://www.lispworks.com/documentation/ ... y/f_eq.htm
-a
-
- Posts: 94
- Joined: Mon Jul 21, 2008 7:26 am
- Location: München, Germany
- Contact:
Re: Questions about how lisp uses 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.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?
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.
Yes, and yes. Implementations behave differently in this regard. In practice, constants are different from parameters in that: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.
- 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).
(Conceptually, literals are actually part of the source code of your program, so by modifying a literal, you modify program code.)