What REALLY happens in memory

When you write:

 
int x = 10;

The computer does three things:

  1. Reserves 4 bytes in RAM (for int)

  2. Stores value 10 in those bytes

  3. Assigns a unique address to that memory

Example (imaginary):

 
Address Value
0x1000 10

Now x means:
“Go to address 0x1000 and read value”

Adding a Pointer

 
int *p = &x;

Now:

 
Address Value
0x1000 10x
0x2000 0x1000 ← p

Key understanding:

  • x stores data

  • p stores address

  • *p means:
     go to address stored in p, then read value

Why *p Works (CPU level)

When CPU sees:

 
*p = 50;

Steps:

  1. Read address stored in p (0x1000)

  2. Go to that memory

  3. Overwrite value with 50

So memory becomes:

 
0x1000 → 50

That is why:

 
cout << x; // 50

No magic. Only memory access.

Pointer Rebinding (Very Important)

A pointer can change where it points, but a variable cannot.

 
int a = 10, b = 20;
int *p = &a;
p = &b;

What changed?

  • Address inside p

  • Not a, not b

Memory:

 
pa10
pb20

This makes pointers dynamic memory tools.

Pointers with Arrays (EXTREMELY IMPORTANT)

What an Array REALLY Is

 
int arr[5] = {10,20,30,40,50};

Memory layout:

 
Address   Value
0x3000     10 

0x3004     20

0x3008     30
0x300C     40
0x3010     50

Array elements are:
• Contiguous
• Same data type
• Fixed spacing

Why arr Behaves Like a Pointer

The name arr means:
 address of the first element

So:

 
arr == &arr[0]

Both give:

 
0x3000

But IMPORTANT:

  • arr is NOT a pointer variable

  • It is a constant memory address

You cannot do:

 
arr++; // ❌ illegal

Pointer Assigned to Array

 
int *p = arr;

Now:

 
p = 0x3000

So:

  • *p → 10

  • *(p+1) → 20

  • *(p+2) → 30

Why (p + 1) Works 

Pointer arithmetic depends on data type size.

If:

 
p = 0x3000

Then:

 
p + 1 = 0x3000 + 4 = 0x3004

Because:

  • int = 4 bytes

CPU automatically multiplies:

 
offset × sizeof(type)

You do NOT add bytes manually.

Equivalence Rule (CORE RULE)

These are IDENTICAL:

 
arr[i]
*(arr + i)
*(p + i)
p[i]

All four mean:
value at memory location of ith element

Pointer Arithmetic 

Allowed Operations

p + n
p - n
p++, p--
p2 - p1

p + p
p * p

Pointer Increment Internals

 
p++;

Means:

 
p = p + sizeof(type)

Not:

 
p = p + 1 byte

Example: Walking Through Array Memory

 
int arr[3] = {10,20,30};
int *p = arr;

Memory walk:

 
*p10
*(p+1) → 20
*(p+2) → 30

CPU calculation:

 
address = base + index × sizeof(int)

This is how arrays work internally.

Pointer Difference (Memory Distance)

 
int *p1 = &arr[0];
int *p2 = &arr[4];
p2 - p1 = 4

Why?

  • Difference measured in elements

  • Not bytes

Used in:

  • Array length calculation

  • Iterators

  • Algorithms

Example 1: Reverse Array Using Pointer Arithmetic

 
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1,2,3,4,5};
int *start = arr;
int *end = arr + 4;
while(start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
for(int i=0;i<5;i++)
cout << arr[i] << " ";
}

This works because:

  • Pointers move through memory

  • No indexing needed

Example 2: Sum Using Pointer Walk

 
int arr[5] = {1,2,3,4,5};
int *p = arr;
int sum = 0;
for(int i=0;i<5;i++) {
sum += *p;
p++;
}

Pointer walks memory sequentially.

Example 3: Modify Values via Pointer

 
int arr[3] = {10,20,30};
int *p = arr;
*(p+1) = 99;

Now array becomes:

 
10 99 30

Memory directly changed.

VERY IMPORTANT MEMORY RULES

• Pointer must always point to valid memory
• Never access beyond array bounds
• Pointer arithmetic outside array = undefined behavior
• Pointer does NOT know array size
• Programmer must control limits