Pointer and Program Efficiency

Pointers can be used to build structures like linked lists and trees, and share data among functions and modules. Otherwise pointers can be avoided and there is little need to use pointers merely for the sake of efficiency. In fact, using pointer can often make the program less efficient as Shang pointers are not really memory addresses and are implemented in an abstract manner to ensure safety and generality.

When a function is called, the value of the argument is passed to the function but the copy is not made until the function call attempts to alter the value of the argument. So if the memory usage and performance cost of copying function arguments has been a concern, one may just design the function such that the argument values are not overwritten.

Similarly, assigning a to b does not cause a copy of a made immediately. Although a to b are supposed to be independent data, they share the same storage until one is being changed. Even when such an event occurs, the copying process is still likely to be very efficient, especially when the variable is a list, table, class, function, class or object, in which case, each component of the copy is just a temporary link to the component of the old variable, except the part that is being updated.

oz 2009-12-22