Understanding Pointers in Computer Programming.
It’s the start of my second week here at The Flatiron School, and over the last 6 days, our iOS class has been going over the foundation of Objective-C, the language that writes up most of the popular iPhone apps we use.
Table of Contents
It’s the start of my second week here at The Flatiron School, and over the last 6 days, our iOS class has been going over the foundation of Objective-C, the language that writes up most of the popular iPhone apps we use.
We've been reviewing the 150 hours of assigned pre-work given to us the month beforehand, and on top of that, we have to write and present a technical blog post every week or so on a topic of our choice. Students like myself who were assigned to present this week were given some slack as to how technical our initial post needed to be.
With that said, I decided to blog about my first impressions of the school as well as a concept that I struggled with ever since I started learning Objective-C.
The concept of pointers in the form of asterisks before declaring methods and variables didn’t quite make sense. I dedicated many hours chipping at the labs and readings every single day from the moment I got accepted, but even then, some of these concepts just wouldn’t stick.
{: class="responsive-image" style="margin:0 auto;width:75%;"}
When we need to store data, our programs have to save information in memory, or RAM of your computer, and we refer to those stored values by their memory addresses. So of course, the solution is to store these values in variables and objects, which are the building blocks that eventually make our cool apps. The catch is having to add what’s called a pointer next to the name we call our object by, because the pointer can simply ‘point’ to the location in memory that the object lives in, without the need for excess copies of the same value.
Note- Objective is the first language that I am formally learning so bear with me if the concepts I mention extend beyond Objective -C or object-oriented programming— I’m sure they do since the language is built on C.
In this example below, we have a sentence in the form of an NSString(contains words and text) named ‘greeting’. The instance of this NSString lives in your computer’s memory somewhere, and the ‘greeting’ pointer _points_ to the address for where its sentence lives.
NSString *greeting = @"Hi, I am really a reference to a memory address."
This concept is still a bit tricky because not all data types such as primitives need pointers- but for the most part, when we declare objects in Objective-C, you can only access them through pointers. The idea is based on memory efficiency and it makes sense. Eventually, we’ll be dealing with objects that are a whole lot bigger and more complex than a simply an NSString. Instead of making copies of the object every time it’s called, pointers simply reference where objects are in memory to access its values.
Like many people, when I try to learn something that doesn’t come second nature, I will naturally try to take experiences and principles that I’m familiar to form some sort of relationship with it is I’m trying to understand. One of our classmates Henry used a great example today to conceptualize how the nested multi-layer process of recursions in functions can relate to the movie, “Inception”, where the storyline takes place in dreams within dreams… within dreams.
I, too, love the film, and as a result, I’ve also taken some themes found in Inception to help me grasps some concepts found in computer programming . One of them is the linking the relationship found in pointers with objects to Cobb’s haunting relationship with Maul, the dead wife that Cobb keeps alive by reliving memories of her. You can see where I’m going this.
Now that we understand pointers, we can see that objects are never really living inside other objects. Instead, they just contain references, or pointers, that knows where its value is stored in memory. Here’s a better visual:
@interface Cobb: Person
@property (nonatomic) Person *wife;
Everyday is something new at Flatiron, and everyday is another series of challenges here. Because of that, I think it’s why all of us here find learning so stressful, but exhilarating. Personally, I fail so fast and so often that the moment I have the slightest Eureka moment and dare celebrate, the next lab/ lecture/concept just slaps me down to earth reminding me that there’s a long way to go. But one thing is certain, these next 3 months will be short of analogies.