Search⌘ K

Variables and Memory

Explore how variables are stored as memory locations in C++ and why identifiers are used instead of direct memory addresses. Understand the concept of memory allocation and how the operating system assigns unique addresses for variables. This lesson prepares you for learning pointers by clarifying the relationship between variables and their memory addresses.

Variables

To understand pointers, you must know how variables are stored in memory.

We have already seen that a variable is a named location in a computer’s memory where we can store our data.

See the program given below!

C++
#include <iostream>
using namespace std;
int main() {
// Declares a variable John of type int
int John;
}

How are variables stored in memory?

Consider the analogy given in the previous lesson. The statement on Line No. 7 says reserve ...