IDRASAcademic OS
Unit 1: C Memory Architecture, Dynamic Allocation & Pointer Mechanics 35 mins study timeINTERMEDIATE

Pointers, Indirection, and Heap Memory Lifecycle in C

Foundational breakdown of computer memory architecture: address-of (&), dereferencing (*), pointer arithmetic scaling, heap allocation via malloc/free, and memory safety.

Verified: Faculty Peer Review Board

Learning Objectives

  • •Visualize computer RAM as a linear array of byte addresses and map C variables to addresses.
  • •Demonstrate the dual roles of the asterisk (*) in declaration vs dereferencing.
  • •Calculate exact memory offsets using pointer arithmetic across diverse C primitive types.
  • •Implement safe dynamic memory allocation on the heap with malloc(), calloc(), and free().
  • •Diagnose segmentation faults, dangling pointers, and memory leaks.

Essential Prerequisites

  • •C fundamental types (int, char, float, double) and their byte sizes
  • •Function parameters and scope (call by value)
Layer 1: Intuition & Why It Matters

The Core Mental Model

“Think of your house and its postal address. A variable 'int x = 10' is the house with 10 people inside. A pointer 'int *p = &x' is a slip of paper with your address written on it. Giving someone the piece of paper (passing by pointer) allows them to walk directly to your house and paint the door red, without needing to duplicate your entire house.”

Why This Exists

Pointers give C its raw speed and hardware-level control. Operating system kernels (Linux, Windows NT), embedded firmware in spacecraft and automobiles, graphics engines, and database storage engines are built directly on C memory pointers.

Beginner Foundation

In high-level languages, memory addresses are hidden. In C, you have full control. '&x' means 'where is x located?'. '*ptr' means 'go to the address inside ptr and see what is inside'.

Micro Concepts Decomposition

MICRO CONCEPT 1Canonical Object

Memory Cells & The Address-Of Operator (&)

Every byte in RAM has a unique numerical memory address. The '&' operator retrieves the memory address where a variable is stored. A pointer variable is simply a variable whose value is the memory address of another variable.

Key Takeaway: Pointers do not store data values directly; they store the physical or virtual memory address where the data resides.
MICRO CONCEPT 2Canonical Object

The Dereference Operator (*) & Indirection

The unary '*' operator accesses or mutates the value residing at the address held by the pointer. Changing *ptr directly mutates the original variable in memory.

Key Takeaway: '*ptr' reads or writes the memory cell that 'ptr' points to.
MICRO CONCEPT 3Canonical Object

Pointer Arithmetic & Data Type Scaling

When you add 1 to an integer pointer (int *ptr), it does NOT add 1 byte. The compiler multiplies the increment by sizeof(*ptr) (typically 4 bytes for int, 8 bytes for double).

Key Takeaway: Pointer arithmetic automatically scales by the byte size of the underlying data type.
MICRO CONCEPT 4Canonical Object

Heap Allocation (malloc, free) & Memory Leaks

Stack memory is automatically managed per function frame. Heap memory is allocated manually via malloc(bytes) and must be returned to the OS using free(ptr). Failure to free allocated heap memory causes memory leaks.

Key Takeaway: Every malloc() must have exactly one corresponding free(); never access memory after freeing it (dangling pointer).
Layer 3 & 4: Formal Specification & Mechanism

Hardware State Machine Architecture

Pointers on 64-bit architectures are 8-byte unsigned integers representing addresses in virtual address space. Virtual addresses are translated by the CPU MMU (Memory Management Unit) via page tables into physical RAM frames. Dereferencing an invalid or unmapped address (like NULL or 0x0) triggers a hardware trap that the OS kernel converts into SIGSEGV (Segmentation Fault).
Execution trace of swap(&a, &b): 1. Main frame: a=5 at 0x1000, b=10 at 0x1004. 2. Call swap(0x1000, 0x1004): Swap frame receives ptr1=0x1000, ptr2=0x1004. 3. temp = *ptr1 (reads value at 0x1000 -> 5). 4. *ptr1 = *ptr2 (writes value from 0x1004 -> 10 into 0x1000). 5. *ptr2 = temp (writes 5 into 0x1004). 6. Swap frame pops; Main frame now reflects a=10, b=5 directly.
Layer 7: Interactive Laboratory

Interactive Simulator

COA • SIMULATIONC Pointers, Memory Addresses & Dereferencing Simulator
Launch Fullscreen Lab
COA • CPU ARCHITECTUREOperand Fetch & Memory Dereference

Addressing Modes & Effective Address (EA) Visualizer

1. Instruction Opcode
LOAD R1, 8(R2)
Mode: INDEXED Addressing Mode
Base register plus index/offset value
2. Address Resolution Unit
DERIVATION FORMULA:
EA = [R2] + Displacement/Offset = 0x1004 + 0x0008 = 0x100C
Resolved EA: 0x100C
Memory Bus Accesses: 1 cycle(s)
3. Final Operand Fetched
0x7777 (MEM[0x100C])
Ideal for array and struct indexing (Array base address + index * element size).
CPU Internal Register FileWord-size: 16-bit
R10x0000General Purpose
R20x1004General Purpose
PC0x0200Program Counter
XR0x0008Index Register
RAM Physical Address SpaceWord Addressable
5200x9999Memory Word
40960x0042Memory Word
41000x2000Memory Word
41080x7777Memory Word
81920x5555Memory Word
Layer 5: Step-by-Step Worked Numerical Example

End-to-End Execution Trace

// Swapping two variables using pointers in C #include <stdio.h> void swap(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } int main() { int a = 42, b = 99; printf("Before: a=%d, b=%d\n", a, b); swap(&a, &b); printf("After: a=%d, b=%d\n", a, b); return 0; }
Layer 6: Active Runtime CodeLab

Step-by-Step Code Execution (C)

Font
main.cGlacier Light
Ln 1 • GCC 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
801 chars • 29 lines • Ln 1UTF-8 • 4 Spaces
Interactive Terminal Shell

Sandbox Terminal Ready

Click Run Code or press Ctrl+Enter to compile and execute.

Common Student Pitfalls & Mistakes

Where Students Lose Marks

❌ Mistake: Dereferencing an uninitialized or NULL pointer: int *p; *p = 10;
✓ Correct Understanding: An uninitialized pointer contains random garbage memory addresses. Dereferencing it causes an immediate crash (Segmentation Fault). Always initialize pointers: int *p = NULL;
❌ Mistake: Using freed memory (Dangling pointer): free(ptr); printf('%d', *ptr);
✓ Correct Understanding: Once memory is freed, it belongs back to the allocator. Set ptr = NULL immediately after free(ptr).
❌ Mistake: Confusing *ptr with ptr: writing ptr = 10 instead of *ptr = 10.
✓ Correct Understanding: ptr = 10 makes the pointer point to memory address 10 (which will segfault). *ptr = 10 writes the integer 10 into the memory cell currently pointed to.
Layer 8: Practice & Knowledge Verification

Active Assessment Quiz

Interactive Assessment EngineQuestion 1 of 1

Pointers, Indirection, and Heap Memory Lifecycle in C — Practice Questions

INTERMEDIATE LevelScore: 0/0

Given 32-bit architecture where sizeof(int) is 4 bytes. If ptr points to address 0x2000, what address does (ptr + 2) point to?

int *ptr = (int *)0x2000;
printf("%p", ptr + 2);
Academic Evaluation Preparation

Viva Examination & University Scoring Strategy

Standard Viva Examination Questions

Q1: What is a void pointer (void *) in C?
Answer: A generic pointer that holds the address of any data type without type-specific constraints. It cannot be directly dereferenced without casting to a specific data pointer type.
Q2: What is the difference between malloc() and calloc()?
Answer: malloc() allocates uninitialized raw bytes leaving garbage values; calloc() allocates memory and initializes all bytes to zero.

How to Write High-Scoring University Exam Answers

Define a pointer. Distinguish between address operator (&) and dereference operator (*). Illustrate memory map showing Stack vs Heap. Explain pointer arithmetic with sizeof() scaling. Provide clean code for dynamic array allocation with malloc(), error checking for NULL, and free().