Section 11k

Aliasing


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Henry McCarty (aka Billy the Kid)

While it doesn't happen a lot, there are times when the same data quantity is addressed with two different names. Just as a William might also be known as Will or Bill, this action is called aliasing. One of those situations can be where the same array is passed into two different parameters in a function. There is another classic issue that will be discussed in a later section of this reference. It does not happen a lot, which is part of the problem. It can be an insidious issue that is hard to find so programmers must always be alert to the possibility of aliasing.

One Way It Could Happen

Suppose a function is created that converts a string of varying cases (i.e., some upper case letters and some lower case letters) to a string with all lower case letters. The code for this function is provided here.

void convertStringToLowerCase( char *destString, char *sourceString )
{
int index = 0;
int size = strlen( sourceString );

while( index < size )
{
destString[ index ] = toLowerCase( sourceString[ index ] );

index++;

destString[ index ] = NULL_CHAR;
}
}

This function employs a strategy of adding a null character (NULL_CHAR) ('\n') to the end of the string as it is being created; this assures that the destination string is always a complete c-style string.

However, even though this extra action is a good strategy, it will lead to issues later. Now, consider the situation where a programmer wishes to convert a string into lower case letters but rather than making the conversion from one string to a different one, the programmer simply wants to convert the current string without involving another one. This is a fair request of the function, and would be called as follows.

convertStringToLowerCase( workingString, workingString );

Again, this should be a reasonable request of the convert string function but consider what will now happen under the hood. Inside the function, workingString (the actual parameter) will be set as destString (the formal parameter) AND it will be set as sourceString (the other formal parameter). Aliasing is now in play since one set of data (the string/array) now has two names.

Now assume that the string is aBcDeF. as shown here.

Array before modification

When tracing the code, the first character ('a') will be assigned as is (i.e., not converted) from the sourceString first element to the destString first element. Then, immediately after the assignment, the index is incremented to one, and then before the loop ends and as previously discussed, a NULL_CHAR ('\n') is assigned to the destString at index one, as shown here.

Array after modification

Now, it is clear how destString has been modified but the problem is that since the array name sourceString is also pointing at the same data, sourceString will now ALSO hold the updated second element containing the NULL_CHAR ('\n'). In fact, tracing the rest of the code will show that the whole rest of the string will be filled with NULL_CHAR ('\n') values and when displayed, the 'a' will be the only thing showing in the string after this process has completed. All strings end when the first NULL_CHAR has been found.

Here is the data result after completion of the function.

Array after function completion

It is pretty clear that the data has been corrupted, and remember, since it is an array, the data will continue to be corrupted after the function has finished. However, as a heads up, this is a pretty insidious bug, and it is not always as easy to find or identify as it was in this example.

Aliasing, the Programming Kind

There are several conditions that can bring aliasing into play. The one provided above is just one. While there will be another example provided later when linked lists are studied, the real key is to watch out for it in any programming that is conducted. Keep an eye on this.