What Is Aliasing in Computer Science? Why Does It Happen? And Is It Bad?

What Is Aliasing in Computer Science? Why Does It Happen? And Is It Bad? Featured Image

A concept common in early computer science courses is aliasing. Alarmingly, however, there seems to be very few resources on the topic online. As a result, I figured I’d take a quit stab at it!

Table of Contents

What Is Aliasing?

Aliasing refers to a scenario when two or more variables refer to the same value in memory. As a result, aliasing can only occur—to my knowledge—in programming languages that support references.

References, for the uninitiated, are variables that store the location of some data. For folks who have worked with lower level programming languages, you can think of a reference as a slightly more opaque pointer. In other words, a reference doesn’t store a value directly, but instead directs you to where that value is stored.

Because references are one step removed from the actual value, situations can arise where multiple variables reference the same value. Therefore, anything that happens to the value will be reflected in every variable.

If it helps you think about aliasing, imagine giving your address to a painter and your friend. Tomorrow, your friend comes over to hang out. Then, a day later, the painter comes and changes your house color from red to blue. When your friend comes back to visit a week later, they’ll see the changes.

To extend the example above, any number of folks could have a reference to your house. Therefore, any changes to your house (e.g., a new couch, a bit of remodeling, etc.) could be seen by these folks. Consequently, any of these folks could make a change to your house. In some cases, this situation is desirable. For example, you probably want the painter to paint your house. In other cases, it’s not quite as desirable. For instance, you probably don’t want your house egged.

Ultimately, the house example doesn’t perfectly model how references work because there are definitely unexpected things that can happen in code. For example, you might want to make a change to your house that you don’t want others to see. In code, you would make a copy of that house. In real life, that’s probably not so practical.

Why Does Aliasing Happen?

In programming, aliasing is generally unintentional. It occurs all the time, and you might not even notice it. For example, every time you pass a reference to a function, that reference is copied to the function parameter. As a result, you end up with two variables pointing to the same value.

Other times, aliasing is a bit more intentional. For example, you might have a set of objects that model a real world family. In a family, items are often shared by the collective. If you were modeling that in code, you might have items like silverware that everyone has a reference to (e.g., everyone knows where the spoons are). It would be impractical to withhold that knowledge from your family and only provide a spoon anytime someone asked for it (though, this example doesn’t really map to code very well).

Finally, there is the form of aliasing that our field tends to hate: aliasing as a result of assignment. In many programming languages, the assignment statement does not perform a copy. Instead, it creates an alias. As a result, while the new variable may appear to hold a copy, it actually holds a reference to the original value. Therefore, any changes to one variable will be seen by the other.

Is Aliasing Bad?

Short answer: maybe.

In general, aliasing is a necessary evil. For example, in functions, copying the reference allows us to make changes to its underlying value. Without that ability, functions could never make changes to a value. Instead, a new value would have to be returned and reassigned to the original variable. You might already be familiar with this kind of behavior with immutable reference types (e.g., strings).

However, there is a particular brand of aliasing that can cause a lot of pain: aliasing by assignment. When variables are unintentionally aliased through assignment, the outcome is most likely going to be bizarre bugs. After all, the intent of using assignment is probably to create a copy, and that is just not how assignment works in many languages.

Therefore, if you want to avoid aliasing, you need to learn how to make copies. Here are a couple resources that cover that subject for my Python and Java users:

Overall, I was a bit surprised by the lack of writing on this subject on the web, so I don’t have any external resources or takes. Though, I’ve seen quite a bit about the issues that aliasing can have on program performance. At any rate, that’s all I wanted to cover today. If you found this article helpful, feel free to show your support by giving it a share and heading over to my list of ways to grow the site. Otherwise, take care!

Coding Tangents (35 Articles)—Series Navigation

As a lifelong learner and aspiring teacher, I find that not all subjects carry the same weight. As a result, some topics can fall through the cracks due to time constraints or other commitments. Personally, I find these lost artifacts to be quite fun to discuss. That’s why I’ve decided to launch a whole series to do just that. Welcome to Coding Tangents, a collection of articles that tackle the edge case topics of software development.

In this series, I’ll be tackling topics that I feel many of my own students have been curious about but never really got the chance to explore. In many cases, these are subjects that I think deserve more exposure in the classroom. For instance, did you ever receive a formal explanation of access modifiers? How about package management? Version control?

In some cases, students are forced to learn these subjects on their own. Naturally, this forms a breeding ground for misconceptions which are made popular in online forums like Stack Overflow and Reddit. With this series, I’m hoping to get back to the basics where these subjects can be tackled in their entirety.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts