Pointers and Memory Basics

A pointer stores a memory address; dereferencing it reaches the value there, pointer arithmetic scales by the pointed-to type’s size, and dynamic memory from the heap must be explicitly freed to avoid leaks.

Key formulas & points

Skim these first — then read the full notes below.

  • Dereferencing a NULL or dangling pointer is undefined behaviour
  • Pointer arithmetic is valid only within one array object
  • Memory regions: stack (auto), heap (dynamic), static/data, code

Topic details

Introduction

This topic explains direct memory access. You learn the address-of and dereference operators, how pointer arithmetic scales by element size, the difference between stack and heap allocation, and the discipline needed to avoid leaks, dangling pointers and undefined behaviour.

Key relations & formulas

Formulas (Indian textbook notation)

  • &x → address of x; *p → value at address p

Formulas (Indian textbook notation)

  • p+kaddressadvancedbyk×sizeof(p)bytesp + k → address advanced by k \times sizeof(*p) bytes

Formulas (Indian textbook notation)

  • malloc(n)heapblock;free(p)releasesitmalloc(n) → heap block; free(p) releases it

Notation and sign conventions

Relation 1 —
&x → address of x; *p → value at address p

Formulas (Indian textbook notation)

  • &x → address of x; *p → value at address p
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
p+kaddressadvancedbyk×sizeofp + k → address advanced by k \times sizeof

Formulas (Indian textbook notation)

  • p+kaddressadvancedbyk×sizeof(p)bytesp + k → address advanced by k \times sizeof(*p) bytes
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
mallocmalloc

Formulas (Indian textbook notation)

  • malloc(n)heapblock;free(p)releasesitmalloc(n) → heap block; free(p) releases it
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Pointers expose the machine’s memory model directly, which is both powerful and dangerous. Because pointer arithmetic advances by whole elements (not bytes), p+1 on an int pointer moves four bytes on a typical system — this is what makes array indexing and pointer traversal equivalent. Stack memory is managed automatically and freed on return, while heap memory from malloc lives until explicitly freed, so forgetting free leaks memory and using memory after free (a dangling pointer) is undefined behaviour. A NULL pointer intentionally points nowhere, and dereferencing it crashes, so defensive code checks pointers before use.

Assumptions and validity limits

State assumptions explicitly before using any relation for pointers and memory basics — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Programming Fundamentals viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Programming Fundamentals papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to pointers and memory basics.
4. Use equation 1:
&x → address of x; *p → value at address p
.
5. Use equation 2:
p+kaddressadvancedbyk×sizeofp + k → address advanced by k \times sizeof
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Pointers and Memory Basics appears in all software development. In Indian it software curricula this topic is tested because it connects theory to core programming concepts in C/C++.
GATE and semester exams often combine pointers and memory basics with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use pointers and memory basics?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students perform byte-based instead of element-based pointer arithmetic, dereference freed or NULL pointers, and leak heap memory by losing the only pointer to it. Confusing the pointer itself with the value it points to is a frequent conceptual error.

Quick revision checklist

Before attempting pointers and memory basics problems, confirm you can:
1. Dereferencing a NULL or dangling pointer is undefined behaviour
2. Pointer arithmetic is valid only within one array object
3. Memory regions: stack (auto), heap (dynamic), static/data, code
Revise the solved examples in Let Us C — Yashavant Kanetkar and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Pointer arithmetic offset

Problem

An int pointer p holds address 1000 (int = 4 bytes). What address does p + 3 give?

Solution

p + 3 = 1000 + 3 × sizeof(int) = 1000 + 3×4 = 1012. Pointer arithmetic scales by the element size, not by one byte.

Conceptual check — Pointers and Memory Basics

Problem

In a Programming Fundamentals semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of pointers and memory basics." What should a complete answer include?

📖 Standard books (India)

  • Let Us CYashavant Kanetkar

    Read: Syllabus unit

    First programming book for many Indian students