Pointers | Skilled.dev


Course launching November 17, 2020

Follow me on YouTube for free coding interview videos.

Users who sign up for the email list will receive an exclusive 75% discount at launch.

Your subscription could not be saved. Please try again.
Your subscription was successful! 🤓

A pointer is a reference to a location in memory, and we can assign this pointer to a variable.

As an analogy, think of a book's table of contents. When you look up a topic, it points to the page that actually contains the content.

With computers, it is easiest to picture a pointer and the object it references in memory as 2 separate entities. When we assign the object to a variable, the variable becomes a pointer to this object's location in memory. We can read or update values on the object through the pointer, and we can set the variable to a new value and it will simply no longer reference the object.

let pointer = { hi: 'world', is: true };
// The object { hi: 'world', is: true } is created in memory
// The pointer variable references/points to the object
// pointer ----> { hi: 'world', is: true }

// pointer accesses the object from memory and requests the value stored for `hi`
// pointer ----> { hi: 'world', is: true } ----> hi
//                                         <---- 'world'

pointer.hi = 'friends';
// pointer reassigns the value for `hi`
// pointer ----> { hi: 'world', is: true } ----> hi = "friends"

pointer = null;
// pointer changes its reference from the object to null
// The object still exists in memory but is just no longer referenced to by the variable
// pointer           { hi: 'world', is: true }
//       |
//       |
//       |--> null

I highly recommend the video because it makes it much easier to grasp what's going on.

Let's take a deeper look by creating 3 objects and assigning them to variables: parent, child1, and child2. Our programming language will create these 3 objects in memory, and the variables we assign them to will reference/point to these memory addresses.

let parent = { age: 45, c1: null, c2: null };
let child1 = { age: 10, mood: 'happy' };
let child2 = { age: 12, mood: 'bored' };

parent.c1 = child1;
parent.c2 = child2;

When we set the parent object's parent.c1 = child1 attribute, we're not creating any new objects or variables. What happens is that c1 points to the same memory slot that child1 points to.

If we were to modify the object's data through one of our variables that is referencing it, the change would be recognized in both since they are referencing the item in memory.

console.log(parant.c1.mood, child1.mood); // happy, happy

child1.mood = 'sad';
console.log(parant.c1.mood, child1.mood); // sad, sad

parent.c1.mood = 'stoked';
console.log(parant.c1.mood, child1.mood); // stoked, stoked

If we delete a reference or reassign the variable to a new value, this will make no changes to the underlying object in memory. All we have done is modify the pointer that was referencing.

parant.c1 = null;
console.log(parant.c1, child1.mood); // null, happy

parant.c1 = child1;

child1 = 'no longer an object pointer';
console.log(parant.c1.mood, child1); // sad, no longer an object pointer

parent.c1 = false;
child1 = null;
console.log(parant.c1, child1); // false, null

If we remove all the references to an object in memory, it will no longer be accessible or used by our program. Our programming language will see memory that was previously allocated is no longer needed and will clear the memory slots it was using.

In higher-level programming languages, this entire process is handled for us. Our runtime will allocate the memory when variables are created and then free the memory (garbage collect it) when there is no longer a variable referencing that location. In some languages like C++ you need to manually clear the memory yourself to free it up. This requires more effort from the developer, but it allows your code to be finely-tuned and optimized to your exact needs.

Implement a Linked List
Combine Blockchains

Table of Contents