IDRASAcademic OS
Unit 1: C Memory Architecture, Stack, Heap & Pointers 35 mins study timeFOUNDATION

Process Memory Layout, Pointer Dereferencing & Indirection Mechanics

Structure of program execution memory in Linux/x86_64, address-of operator (&), indirection operator (*), and pointer arithmetic.

Verified: Faculty Peer Review Board

Learning Objectives

    Essential Prerequisites

      Layer 1: Intuition & Why It Matters

      The Core Mental Model

      “Pointers ko samajhne ke liye ek real-world hotel ka example lijiye: Har hotel room ka ek Unique Room Number (Address) hota hai, aur room ke andar ek Mehmaan (Value) hota hai. - Normal variable: jaise room ke andar baitha mehmaan ('int a = 10;'). - Pointer variable: ek aisi diary jisme room ka number likha ho ('int *ptr = &a;'). Jab aap diary me likha room number dekhte hain to aapko address milta hai ('ptr'). Aur jab aap hotel room ka darwaza khol kar andar dekhte hain ('*ptr'), to aapko asli mehmaan (10) milta hai! Isko kehte hain Dereferencing! Sabse badi galti: Segmentation Fault! Sochiye aap kisi aise room number ka darwaza kholne ki koshish karein jo hotel me exist hi nahi karta (jaise NULL ya uninitialized pointer), to security guard (Operating System) aapko turant hotel se bahar nikal dega (Crash / Segfault)!”

      Why This Exists

      Operating systems (Linux kernel), embedded microcontrollers, and databases like SQLite and PostgreSQL are written in C because of direct hardware pointer manipulation.

      Beginner Foundation

      Pointers ko samajhne ke liye ek real-world hotel ka example lijiye: Har hotel room ka ek Unique Room Number (Address) hota hai, aur room ke andar ek Mehmaan (Value) hota hai. - Normal variable: jaise room ke andar baitha mehmaan ('int a = 10;'). - Pointer variable: ek aisi diary jisme room ka number likha ho ('int *ptr = &a;'). Jab aap diary me li...

      Micro Concepts Decomposition

      MICRO CONCEPT 1Canonical Object

      Process Virtual Address Space Layout

      A running C binary divides into Text (machine instructions), Initialized Data (.data), Uninitialized Data (.bss), Heap (dynamic allocations growing upward), and Stack (local stack frames growing downward).

      Key Takeaway: Stack memory is automatically reclaimed upon function return, whereas Heap memory persists until explicitly released via free().
      MICRO CONCEPT 2Canonical Object

      Pointer Indirection & Memory Offsets

      A pointer stores a 64-bit hexadecimal memory address. Dereferencing (*ptr) reads or writes the bytes at that address based on the type's size.

      Key Takeaway: Pointer arithmetic scales implicitly by sizeof(type). Incrementing an int* moves forward by 4 bytes, not 1 byte.
      Layer 3 & 4: Formal Specification & Mechanism

      Hardware State Machine Architecture

      In the x86_64 architecture, a user-space C process is allocated a 48-bit canonical virtual address space (0x000000000000 to 0x7FFFFFFFFFFF). The address space consists of: 1. Text Segment: Read-only, contains binary instructions. 2. Data Segment: Global and static variables initialized by programmer. 3. BSS Segment: Block Started by Symbol, zero-initialized globals. 4. Heap: Managed via brk/sbrk and mmap system calls, expands towards higher memory. 5. Stack: Managed via RSP register, stores stack frames (return addresses, frame pointer, local variables), grows towards lower memory. Pointer declaration syntax T* p binds p to an address where sizeof(T) bytes will be accessed. Given pointer p of type T*, the expression p + k evaluates to: Address(p + k) = Address(p) + k * sizeof(T).
      1. Compiler allocates 8 bytes on stack for pointer variable p. 2. Address-of operator (&var) calculates effective address relative to RBP frame pointer. 3. Assignment stores the virtual address into p. 4. Indirection (*p) generates a mov instruction with base-displacement addressing [rax]. 5. OS MMU converts virtual address to physical DRAM page via multi-level page tables.
      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

      Trace pointer arithmetic: int arr[4] = {100, 200, 300, 400}; // Located at address 0x1000 int *p = arr; // p = 0x1000, *p = 100 p++; // p = 0x1000 + 1 * 4 = 0x1004, *p = 200 *(p + 2) = *(0x1004 + 8) = *(0x100C) = arr[3] = 400.
      Layer 6: Active Runtime CodeLab

      Step-by-Step Code Execution (C)

      SQL Studio
      Font
      main.cGlacier Light
      Ln 1 • GCC 13
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      205 chars • 15 lines • Ln 1UTF-8 • 4 Spaces
      Interactive Terminal Shell

      Sandbox Terminal Ready

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

      ⚡ AURXON Bitstream Runtime v4.8IDRAS Academic Virtual Node
      Layer 8: Practice & Knowledge Verification

      Active Assessment Quiz

      No Practice Questions Configured

      Questions for this topic are currently undergoing faculty review.

      Academic Evaluation Preparation

      Viva Examination & University Scoring Strategy

      Standard Viva Examination Questions

      How to Write High-Scoring University Exam Answers

      Draw the 5-layer process memory diagram with Stack and Heap expansion vectors, define pointer declaration and dereferencing mathematically, demonstrate call-by-reference with a swap function, and explain 3 categories of pointer bugs.