IDRASAcademic OS
CS305 • CANONICAL ACADEMIC TEXTBOOK1 Units • 3 Topics • Verified Multilingual Labs

Systems Programming in C: Digital Knowledge & Laboratory Textbook

Low-level memory management, pointers, bitwise manipulation, system calls, and performance optimization.

Table of Contents1 of 3
Unit 1: C Memory Architecture, Dynamic Allocation & Pointer Mechanics
Unit 1 • Chapter 1Estimated Study Effort: 35 minsINTERMEDIATE

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.

Learning Outcomes & Core 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."]
Conceptual Intuition and Real-World Mental Model (Hinglish)

What Problem Does This Architecture Solve?

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.
Formal Technical Definition and Notation

Rigorous Specification, Assumptions and Invariants

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).
Step-by-Step State Transition and Mechanism

Execution Trace and State Mutation Sequence

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.
Worked Numerical and Dry-Run Walkthrough

Step-by-Step Numerical Example with Edge Cases

// 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; }
Interactive Code Laboratory
main.cc
// Dynamic Array Allocation and Memory Safety in C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int n = 5;
    // Allocate heap buffer for 5 integers
    int *arr = (int *)malloc(n * sizeof(int));
    if (arr == NULL) {
        fprintf(stderr, "Fatal: Out of memory allocation failed\n");
        return 1;
    }

    // Initialize buffer using pointer arithmetic
    for (int i = 0; i < n; i++) {
        *(arr + i) = (i + 1) * 10; // Equivalent to arr[i]
    }

    printf("Dynamic Heap Buffer Values:\n");
    for (int i = 0; i < n; i++) {
        printf("arr[%d] at address %p = %d\n", i, (void *)(arr + i), arr[i]);
    }

    // Always release allocated heap memory to prevent memory leaks
    free(arr);
    arr = NULL; // Defend against dangling pointer dereference
    return 0;
}

Canonical Micro-Concepts

Concept #1Academic Micro-Unit

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.

Core Takeaway: Pointers do not store data values directly; they store the physical or virtual memory address where the data resides.
Concept #2Academic Micro-Unit

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.

Core Takeaway: '*ptr' reads or writes the memory cell that 'ptr' points to.
Concept #3Academic Micro-Unit

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).

Core Takeaway: Pointer arithmetic automatically scales by the byte size of the underlying data type.
Concept #4Academic Micro-Unit

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.

Core Takeaway: Every malloc() must have exactly one corresponding free(); never access memory after freeing it (dangling pointer).
Production Systems and Industrial Engineering Relevance

How This Concept Powers Real-World Tech Infrastructure

Essential for low-level systems programming, Linux device driver development, hypervisors, and writing high-frequency trading engines in C/C++.

Topic 1 of 3