Nerd For Tech
Published in

Nerd For Tech

A little recap of the previous post

We built a data structure that stores a group of numbers in contiguous memory locations in RAM. We gave it a fancy name called ‘Array’.

Since they are stored in contiguous memory locations, they are positioned sequentially. Such positions are called ‘Array Indices’.

Enough of recap. Let us expand the horizon a little bit

What if we want to store a ‘series of characters’?

Series of characters = ‘String’ in the computer world. Note: Characters include special characters, spaces etc.

Okay, let’s store ‘Finch’…But wait! Computer can understand only in terms of 0 and 1(Binary). I need to find a way to convert the Characters into Binary.

To do this we will map each known character to numbers(See ASCII table)
Then numbers can be easily converted into Binaries. What we did above is called ‘Character encoding’(Just jargon).

Characters — ASCII — Binary

Time for real action. Let’s store

We have series of characters ‘F’ ‘i’ ’n’ ‘c’ ‘h

Note here:

Each character fits one byte.
They are all stored in a contiguous memory location.
All are of the same data type.

Therefore, we can comfortably say it is an ‘Array

Simple! Okay then let’s complicate

What if we want to store ‘series of series of characters’

Doesn’t make sense?
See, series of characters = ‘String’
Series of series of characters = ‘series of strings’

It’s like storing names like ‘Finch’,’ Patrick’ etc

No of characters in ‘Finch’ = 5
No of characters in ‘Patrick’ = 7

Illustrates wasted space

For an array,
Do we need to have some type of data? In this case, it is a character →No problem!

We need to allocate an equal number of spaces for both the strings →Problem because(5 != 7).
For time being we will allocate the maximum for all(In this case 7).
Note: Here for the first index(finch) →2 bytes are wasted

We need continuous memory allocations →If available okay but we need to have another plan to store it. Isn’t it?

No continuous memory locations?

What we have faced now is space constraint?

We have 12 bytes but not in a continuous location

So we store the values wherever possible.
Then we will form an array that contains the memory locations of these characters.

An array that contains only memory locations that points to character

This array essentially contains memory locations in sequential order.
Memory locations are of integer.
We have no problem with the type of data, equal space(64 bit), continuous location.

The array we built to overcome the space problem is called ‘Pointer’.

Pointer just points to memory
RAM distinguishes pointer from normal integer by identifying star in front of the number

I think we overstuffed today. Let’s finish here

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arjun Vaidy

Arjun Vaidy

Founder of a startup. I explain things through first principles and intuitive mental models