Memory
Tags: computerscience, interview
Published: 2022-12-03 04:47:21

Memory is a broad topic. There are a lot of computer science courses, videos, and lessons that are relevant to memory. For the sake of time, This blog will cover memory in the context of technical/coding interviews.

## Introduction
The very basic thing we can do in programming is declare variables. We declare variables to store in some kind of data and use them later in our program. That variable has to be stored somewhere in the computer to be referenced later on in our code. This is where memory comes into play. 

## What is Memory?


![Memory representation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpm62tsu04ybhrphl0um.jpg)

This is a basic representation of memory. It's divided into a lot of little memory slots. These memory slots are identified by a unique number known as memory addresses. 


![Memory address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ut4qp309pocyl5hjso9x.jpg)

There are a finite number of memory slots, meaning it is bounded. If every memory slot is occupied, we wouldn't have any available memory slots to store data. Let's look at an example: 

```Javascript
const num = 73;
```

Here we have a variable with the value of the number 73. Our program will store the value of the variable in a memory slot. Any memory slot that is **NOT OCCUPIED.** 



![Memory value](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0einr3iuvyc2t9y7iza.jpg)

Easy right? let's dive deeper. 

## Bit, Byte, Fixed-width integer

Turns out, when we store data in a memory slot, we don't store them by their value, because computers only understand 0s and 1s. When we store data, we actually store them in the form of **bits**.

**A bit** is the smallest unit of data that a computer comprehends. A bit is always represented by a 0 or 1. So for our previously declared variable, we store the data in the form of **bits** instead of the value 73.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbgd2ourzjd44hxaxv52.jpg)

 A group of 8 bits is called a **byte.** As an example: **01001010**. This is considered a byte, because it has 8 bits of 0s and 1s. If we have more than 8 bits, as an example, in the case of 16 bits, we would divide 16 by 8 to convert it to the number of bytes, which would give us 2 bytes. In the case of 32 bits, we would divide 32 by 8 bits, which would give us 4 bytes. 

![bits data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st0z3slo9o0amuuh9n8s.jpg)

**A single memory slot can only hold eight bits or one byte**, if we have more than one byte, our program needs to store the data in back-to-back or contiguous **free** memory slots. 


Different data types are represented by a different number of bits. If you come from C++ or Java background, you might know that **int** data type is represented as a 32-bit integer, and a **long** data type is represented as a 64-bit integer. These are known as **Fixed-width integer**, which are integral data types with a fixed number of bits. Anything that is more than 8 bits or 1 byte will take up more memory slots. For example, our previously declared variable has a value of 73, which is an integer. We know that an integer is represented as a 32-bit integer. 32 / 8 will give us 4 bytes. This means that our program will need a total of 4 free memory slots to store the data **since one memory slot can only hold 1 byte.**

- This is the 32-bit representation of of our previously declared variable 73.

`00000000 00000000 00000000 01001001`

- This is what it looks like when stored in our memory.

![back-to-back](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pazosyh5lta8yl9jfby.jpg)

> One important thing to note is that when we are dealing with integers, the number of memory slots we're going to be using is always constant. The number of memory slots **will not** increase or decrease if our number increases or decreases. if we declare a 32-bits integer, it will always take 4 memory slots, and if we declare a 64-bits integer, it will always take 8 memory slots.

It also works the same way if we have an array/list. Our program takes contiguous memory slots to store the data.

## How are strings stored in memory?
Well, we can actually convert or map a character into an integer. This method is known as ASCII character mapping. ASCII is basically a table that assigns a bunch of characters to a number. As an example, the letter 'A' is represented as 65 in the ASCII code. This means that declaring a variable and assigning it a value of 65 and declaring another variable and assigning it a value of the letter 'A' are the same in terms of bits.

> Nowadays, Unicode is mostly used to store text data. Unicode is similar to ASCII, however, Unicode represents far more characters than ASCII. It supports a larger range of characters, which takes up a lot more space than ASCII. Unicode encodes emojis as well.


![ASCII](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uig1i7rog7njx0yrmpbk.jpg)


## Advanced
So far, we've only stored data types like integers and strings in our memory slots/addresses. However, that is not the end line. We can actually store the memory addresses of another memory address/slot. As an example, we can store the memory address 18 at the memory slot 2. This is referred to as a **pointer**. A pointer from one memory slot/address to another memory slot.

![pointer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt1wzp5dhu7efci045sa.jpg)

## Conclusion
Memory is the place where information is stored for immediate use. Memory is one of the basic functions of a computer, because without it, a computer would not be able to function properly.

Thank you! 
[Jonny](linkedin.com/in/jonnytilahun)