================================= DEFINITIONS ============================== Value: In computer science, a value is a sequence of bits that is interpreted according to some data type. Reference: In computer science, a reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. Accessing the value referred to by a reference is called dereferencing it. References are fundamental to constructing many data structures (such as linked lists) and in exchanging information between different parts of a program. Pointer: In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). ==================================================================== * Call-by-value. Pass the r-value of A[j]. * Call-by-reference. Pass the l-value of A[j]. * Call-by-name. Pass the text of A[j] itself, avoiding "name clashes." ================================= C ================================= pass by value semantics SYNTAX: int a = 5; int *money = NULL; money = &a; *money = 8; malloc(int variable) free(pointer) YOU CAN PASS BY REFERENCE: void alter(int *n) { *n = 120; } void func(void) { int x = 24; alter(&x); } HUGE BUGS: int *ptr = malloc(sizeof (int)); free(ptr); *ptr = 0; /* Undefined behavior! */ ------------------------- int array[5]; /* Declares 5 contiguous integers */ int *ptr = array; /* Arrays can be used as pointers */ ptr[0] = 1; /* Pointers can be indexed with array syntax */ *(array + 1) = 2; /* Arrays can be dereferenced with pointer syntax */ ================================= C++ ================================= pass by value or reference A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object. & Reseat it? No way. You can't separate the reference from the referent. Unlike a pointer, once a reference is bound to an object, it can not be "reseated" implementation: Underneath it all, a reference i to object x is typically the machine address of the object x. But when the programmer says i++, the compiler generates code that increments x. Difference in C++: There is a simple conversion between pointers and references: the address-of operator (&) will yield a pointer referring to the same object when applied to a reference, and a reference which is initialized from the dereference (*) of a pointer value will refer to the same object as that pointer, where this is possible without invoking undefined behavior. weird: int& preinc(int& x) { ++x; return x; } preinc(y) = 5; // same as ++y, y = 5 What does "const Fred* p" mean? = Fred const* x It means p points to an object of class Fred, but p can't be used to change that Fred object (naturally p could also be NULL). What's the difference between "const Fred* p", "Fred* const p" and "const Fred* const p"? You have to read pointer declarations right-to-left. * const Fred* p means "p points to a Fred that is const" — that is, the Fred object can't be changed via p. * Fred* const p means "p is a const pointer to a Fred" — that is, you can change the Fred object via p, but you can't change the pointer p itself. * const Fred* const p means "p is a const pointer to a const Fred" — that is, you can't change the pointer p itself, nor can you change the Fred object via p. const Fred& x = Fred const& x It means x aliases a Fred object, but x can't be used to change that Fred object. Fred& const x -> stupid const std::string& name_good() const; ← Right: the caller can't change the name std::string& name_evil() const; ← Wrong: the caller can change the name ====================================== Java ================================ Java is weird. public void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } public void swap(Integer var1, Integer var2) { Integer temp = var1; var1 = var2; var2 = temp; } X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0 public static void main(String [] args) { Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); } ================================ Python & Lisp ============================= You could think of Python as a Java only having reference types. Rather, Python works like LISP. That is: Python names are pointers bound to values. Python always pass pointers to values. Python never passes local copies. Function arguments are referenced by names in the function's local namespace. Names referencing function arguments can be rebound in the local scope. def foobar(a): a = 0 # rebinds a locally, produces no side-effect a = 1 # a points to an int(1) foobar(a) # a still points to an int(1) as an int is immutable a = [1, 2, 3] # rebinds a, a points to a mutable type foobar(a) # a points to [1,2,3,1,2,3] >def foobar(a): > a *= 2 # rebinds a only if a points to an immutable type, > # otherwise the value pointed to by a is changed