Memory Model in C++

Learn to visualize memory in C++ and understand pointers intuitively

10 minutes

What you will learn

What is computer memory and why do we need it

What is a memory model and how is it useful

How to visualize memory for a C++ program

What is the memory model in C++ and how is it useful

What are bits and bytes and how are they stored

How do C++ program store their data in memory

To be a better programmer, it is very important to have a good understanding of the memory model in the programming language you work in. In this article, we will look at a very simple memory model for C++ that will help you understand everything from pointers to memory management in C++ easily.

What is memory?

A memory model is just an abstraction on top of the real internal working of computer memory. Before we start getting into the details of the model, let's first understand what is a computer memory and why we need it.

Let us understand this by an example of adding two numbers. We know that the CPU can add two numbers and give us the result, but where does the result go?

Loading Image

The CPU can add two numbers and give us the result

There needs to be someplace to store this result and get it back in the future when we need it for some other operations. This is exactly why we need computer memory. Just like the CPU, computer memory (RAM) is also a complex chip. This chip however has a different set of operations than the CPU. Unlike the CPU which does computational operations like addition, subtraction, etc the primary operation of the memory is to store data.

Loading Image

The computer memory (RAM) is yet another chip

The computer memory (RAM) is yet another chip and so it only understands digital signals in the form of high(1) and low (0) voltage. In order to work with computers, we need to convert everything(including data) into 0s and 1s. Fortunately converting data into 0s and 1s is quite simple. All the real-world data can be easily converted 0s and 1s by converting them into their binary form either directly or by applying different encodings. Numbers can be converted to their binary form by converting them to base 2. Non-numeric data can first be encoded into numbers and then into binary form.

The memory model

When writing programs in C++, we should not have to worry about the low-level details of the computer memory. A memory model is an abstraction over the real working of the computer memory that makes it easy for us to understand how our code interacts with the memory. It helps us understand how our data is stored, retrieved and updated and gives us a very good mental picture of how our program interacts with all its data.

You can imagine the memory as a very very long chain of numbered boxes starting from 0 and ending at n-1, where n is the total number of boxes. Let us look at what the memory looks like -

Loading Image

Memory model in C++

So as you can see from the diagram the memory is just a large number of boxes that can hold data. Each box you see there is made up of 8 bits. You can imagine each bit as a simple switch that can be either on(1) or off(0) and so a computer memory is just a very very long sequence of bits which are partitioned in groups of 8. A group of 8 bits is called a byte which is a basic unit of data, just like we have a meter for distance.

  • 1 bit is a binary digit that can either be 0 or 1
  • 1 byte is made up f 8 bits

We can easily store data in the memory but to retrieve it we need to uniquely identify each byte to figure out where to look for the data. The simplest way to uniquely identify every byte is to use its relative position from the first byte as its identifier. This unique identifier of a byte is also called its address

Address in memory
The address of data in memory is the first byte from where the data starts

This memory is connected to the CPU, which during the course of execution of instructions uses the memory to store and retrieve data. The two go side by side just like your intelligence which helps you break down complex problems and your memory which helps you retain your thoughts.

Program Execution

When we run a C++ program by running the machine code that the compiler generates from our C++ code, the entire machine code is first loaded in the memory. The CPU executes our program by reading instructions one by one from the memory starting from the first address that has our code. 

Loading Image

Execution of a C++ program

The memory other than holding our program itself also holds all the data that this program creates/manipulates. This memory model is really useful in understanding the inner working of storage and retrieval of primitive data types like integers, characters and floats as well as the interaction of pointers with data.

Do you want to master data structures?

Try our data structures learning path made of highly visual and interactive courses. Get hands on experience by solving real problems in a structured manner. All resources you would ever need in one place for FREE

Loading ...