2024-10-21
/
Building Cleo

Part 1: Memory: the low level basics

A computer keyboard with 'Computer Science 101' written above it.
IN THIS ARTICLE:

If you’re new here, you should read the introduction to this course. If you’re not new, welcome back!

Okay, this one is going to be pretty boring, but extremely important for understanding the future material.

What is computer memory?

In the end, it’s an extremely long sequence of zeroes and ones, very commonly known as bits. A bit is either a 0 or a 1. Eight bits form a byte. Eight is convenient because it’s a power of 2, and everything about (regular, not quantum!) computers MUST have something to do with 2 (because a bit has two possible values).

Addresses

It would be absolutely impossible to deliver post in a city if our houses didn’t have addresses. So is the deal with memory.

Technically, memory is just a very long sequence of bits, but each item in that sequence has an address, like an index in an array. Except these indices usually look really weird, like 0x7FFF5FBFFD98

It’s weird because it is a hexadecimal number, i.e. a number represented not only by Arabic digits, but also by letters A-F.

In hexadecimal notation, 1 is 1, 10 is 10, 11 is A and 16 is F. 17 is 1F, and 32 is FF. The notation is convenient because 16 is yet another power of 2, so it’s easier to convert between hexadecimal and binary numbers, whilst 0x7FFF5FBFFD98 is infinitely more pleasing to the eye than 011111111111111101011111101111111111110110011000 or 140734799805848 (yes, I never thought I’d ever begin a sentence with “0x7FFF5FBFFD98 is infinitely more pleasing to the eye”)

So, a memory address is basically the index of a bit in the memory array, except the numbers are usually formatted funny, and they are much bigger than what you’re used to - that’s because your computer’s memory is one big array. 16GB of memory? That’s 16 * 1024 * 1024 * 1024 * 8 = 137,438,953,472 bits. Please don’t create  arrays this big in production 🙏

What is stored in memory?

Very good question. Technically, almost everything, except the things that the CPU currently needs to do its calculations. Those things are still stored in memory, but the CPU does have to copy them to actually be able to perform the calculations. Back to the point.

a = true

To execute this very simple line of code, the computer would have to pick an address for the variable a , then store the bit with value 1 in it.

a = true
b = true

This is an almost equally simple line of code, but now the computer would have to pick two addresses, for a and for b. Let’s assume that our computer is very simple, so it will put those variables in adjacent houses, with addresses 1 and 2.

a = 1
b = 2

Okay, this time we are dealing not with boolean values, but with something numeric. Let’s assume that we are on a 64-bit system,

👉(that’s the modern architecture, as opposed to 32-bit that was widespread just a few years ago. Wait, 2010 was almost 15 years ago? I’m not old, you’re old!)

where an integer type takes whopping 8 bytes, or 64 bits. Our computer would put a in house number 1, but in reality a would take up house numbers from 1 to 64. And b would take up house numbers from 65 to 128.

At this point, I am not going into detailing how exactly a single entity takes up this many houses. But I might in the future.

Also, this is not at all how Ruby memory works, but I like its syntax, so I’m rolling with it.

Arrays

a = [true, false]
b = [false, true]

Woah, we’re introducing arrays. While in reality an array in high-level language like Ruby or JavaScript is going to be something a bit more complicated than what I am about to explain, it’s not going to be hugely different.

Our computer would put a in houses 1 and 2, (1 in 1, 0 in 2) and b in houses 3 and 4 (0 in 3, 1 in 4). You, the programmer, would have to take care and remember that house 1 is the beginning of an array of length 2, and house is 3 the beginning of an array of length 2.

The problems begin

Storing arrays is fine. Unfortunately, the real life sometimes demands us to manipulate said arrays. Most often, you have to do it live, with a bunch of interviewers watching as you do it. Urgh, that’s the worst.

Good news! Your task is very simple, here you go

a = [true] * 1024 # an array consisting of 1024 trues
b = [true] # an array consisting of 1 true

Your task is to prepend a single false to a.

a.unshift(false)

Bad news! You’re not doing it in Ruby, you’re the simple computer we’ve talked about earlier.

a is already taking up houses from 1 to 1024. b is taking up house 1025. You cannot create house 1024a to move the new tenant in.

So, what would you have to do?

You would have to use house 1026 to put the new false in, then you would have to move everyone from houses 1 to 1024 into houses 1027 to 2051. And now you’re also left with 1024 empty houses.

That doesn’t seem ideal. And of course computer science nerds have invented a solution long ago.

Linked lists.

This is going to be the topic of the next post in the series, stay tuned (or not, if I have bored you to death already, sorry about that!)

FAQs
Still have questions? Find answers below.
Written by

Read more

Picture of Redux and Typescript logo in front of code
Building Cleo

Using Redux with Typescript for superior type safety

The benefits of Typescript in a team environment for building and maintaining production-worthy codebases are nothing new. At Cleo we committed early to developing our web and mobile apps with Typescript to give us the safety of static typing when developing our product.

2020-10-14
Illustration of bugs
Building Cleo

How to fix bugs with Norse mythology

You didn’t hear it from me, but Cleo Engineers aren’t perfect. We have bugs. We have quite a few of them. Our relentless focus at Cleo is to make it as simple and joyful as possible for our users to level up their relationship with money.

2021-02-23

signing up takes
2 minutes

QR code to download cleo app
Talking to Cleo and seeing a breakdown of your money.