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.)