Aliasing is a phenomenon that occurs when two names refer to the same resource.
Aliasing happens in both languages with references and languages with pointers.
Aliasing can be controlled with:
Consider the following Java program:
void transpose(int[][] X, int[][] Y) {
for (int i = 0; i < X.length; i++) {
for (int j = 0; j < X[0].length; j++) {
X[i][j] = Y[j][i];
}
}
}
The program represents matrices of integers as nested arrays. The aim of the code above is to transpose the matrix Y
and put its result into the matrix X
. Now suppose we initialise a matrix, and we make the following call:
int[][] Z = ...; // initialise matrix
...
transpose(Z, Z);
As arrays in Java are passed as references, the formal parameters X
and Y
will refer to the same nested array, so the call will malfunction.
A similar example applies to a language with pointers.
For example, in C we could have
int **p = ... // initialise matrix
...
transpose(p, p);
Reynolds, John C. 1978. ‘Syntactic Control of Interference’. In Proceedings of the 5th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages, 39–46. New York, NY, USA: Association for Computing Machinery. https://doi.org/10.1145/512760.512766. [pdf]
@inproceedings{reynolds_1978,
address = {New York, NY, USA},
title = {Syntactic {Control} of {Interference}},
doi = {10.1145/512760.512766},
booktitle = {Proceedings of the 5th {ACM} {SIGACT}-{SIGPLAN} {Symposium} on {Principles} of {Programming} {Languages}},
publisher = {Association for Computing Machinery},
author = {Reynolds, John C.},
year = {1978},
pages = {39--46}
}