Pointers and Memory Practice for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

Pointer questions show a few lines of C or C++ and ask what they print, which address an expression holds, or why a program misbehaves. You need two skills: tracking addresses and values side by side, and knowing which memory (stack or heap) each object lives in and for how long. Programming carries 10% of the NSCT according to HEC's Areas of Competencies, and candidates averaged 47.8% accuracy in it in 2026, per the HEC NSCT report.

The official Memory Management Concepts topic covers leaks, RAII and garbage collection. This page drills the tracing. No C/C++ compiler was available, so each program was traced by hand and checked against a Python model of its memory. The Python examples were run directly. All examples assume a 64-bit system with 4-byte int, 8-byte double and 8-byte pointers, and none relies on undefined behaviour.

The method, step by step

  1. Draw a memory table with columns for the name, address and value. Pointers hold addresses, so write the arrow down.
  2. &x means the address of x. *p means the value at the address in p.
  3. Pointer arithmetic moves by elements: p + k adds k × sizeof(*p) bytes.
  4. Watch precedence: *p++ is *(p++), and (*p)++ increments the value.
  5. For function calls, remember that everything is copied, pointers included. Only a write through *p (or a reference) reaches the caller.
  6. For lifetimes, a local variable dies when its function returns. A new or malloc block lives until delete or free.

Pointer basics: & and *

int x = 25;
int *p = &x;     // p holds the address of x
*p = 30;         // writes through p: x is now 30
int **pp = &p;   // pp holds the address of p
Expression Meaning Value
x the int 30
&x, p, *pp address of x same address
*p, **pp value at that address 30
&p, pp address of the pointer itself a different address

Const placement is read from right to left. const int *p cannot change the value but can move. int *const p cannot move but can change the value.

Pointer arithmetic

Worked example. int arr[] = {10, 20, 30, 40, 50}; starts at address 0x1000.

Expression Address Value
arr, &arr[0] 0x1000 10
arr + 3, &arr[3] 0x1000 + 3 × 4 = 0x100C 40
arr + 5 (one past the end) 0x1014 must not be dereferenced

After int *p = arr; p += 2;, *p is 30, p[1] is 40 and p[-1] is 20. If q = &arr[4], then q - p is 2 elements (8 bytes). A double* moves 8 bytes per step and a char* moves 1.

Worked example (increments). With int a[] = {3, 7, 11}; int *p = a;:

Statement Effect Result
int x = *p++; read a[0], then move p to a[1] x = 3
int y = (*p)++; read a[1], then a[1] becomes 8 y = 7
int z = ++*p; a[1] becomes 9, then read it z = 9

Final state: a = {3, 9, 11} and p − a = 1.

2D arrays are stored row by row. In int m[3][4], m[r][c] sits (r × 4 + c) ints from the start, so m[2][1] is 9 ints (36 bytes) in. *(*(m + r) + c) is the same as m[r][c].

Memory diagram of int arr[] = {10, 20, 30, 40, 50} starting at 0x1000 with 4-byte ints: arr + 3 points to 0x100C holding 40, and arr + 5 is one past the end at 0x1014 and must not be dereferenced.
The page's array example. Adding 1 to an int pointer moves it by sizeof(int), here 4 bytes.

Stack vs heap

The stack holds each function call's frame: parameters, local variables and the return address. The frame disappears automatically when the function returns. The heap holds blocks you request with new or malloc, which live until you release them.

Worked example.

int* makeSquares(int n) {
    int* a = new int[n];               // a: stack; the n ints: heap
    for (int i = 0; i < n; i++) a[i] = i * i;
    return a;                          // frame popped; heap block survives
}
int main() {
    int* data = makeSquares(4);        // data: stack (main's frame)
    int sum = 0;
    for (int i = 0; i < 4; i++) sum += data[i];
    delete[] data;                     // release the block
    data = nullptr;                    // no dangling pointer left
}
Object Lives in Dies when
n, a, i in makeSquares stack makeSquares returns
the 4 ints heap delete[] data
data, sum stack main returns

Answer: sum = 0 + 1 + 4 + 9 = 14. If makeSquares had returned the address of a local array, the pointer would dangle after the return. Using it would be undefined behaviour.

Stack Heap
Allocation automatic, on function call explicit new/malloc (or GC-managed in Java/Python)
Release automatic, on return delete/free, smart pointers or GC
Size small and fixed per thread large
Typical bug stack overflow from deep recursion leaks, dangling pointers, double free

Passing pointers and references

void swapP(int *a, int *b) { int t = *a; *a = *b; *b = t; }
void swapV(int a, int b)   { int t = a; a = b; b = t; }

With x = 3 and y = 8, swapP(&x, &y) leaves x = 8 and y = 3. swapV(x, y) leaves them unchanged, because only the copies were swapped. A C++ reference parameter (int &a) behaves like swapP with cleaner syntax. A reference can never be re-seated, so r = y copies a value.

Python: names, references and copies

Python has no pointer arithmetic, but every variable is a reference to an object.

a = [1, 2, 3]
b = a          # same list
c = a[:]       # new list (shallow copy)
b.append(4)
c.append(5)
print(a, b, c, a is b, a is c)
# [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 5] True False

copy.copy shares nested objects and copy.deepcopy does not. After grid[0][0] = 99, a shallow copy shows 99 and a deep copy still shows 1.

Speed tricks

Common mistakes

Practice set

Use int arr[] = {2, 4, 6, 8, 10, 12}; for questions 1–4.

  1. With int *p = arr + 1;, what is *(p + 2)?
  2. If arr starts at address 500, what is &arr[5]?
  3. With int *s = arr, *e = arr + 5;, what is e - s?
  4. What is sizeof(arr) / sizeof(arr[0])?
  5. After int x = 7; int *p = &x; *p = 10;, what is x?
  6. What is sizeof(double*) on the assumed system?
  7. Python: t = (1, [2, 3]); t[1].append(4); print(t). What happens?
  8. What is sizeof of char s[] = "PAK";?
  9. Which one leaks memory: int *p = new int(5); p = new int(6); or int *p = new int(5); delete p; p = new int(6);?
Show answers
  1. 8. p is at arr[1], so p + 2 is arr[3].
  2. 520. 500 + 5 × 4.
  3. 5 elements.
  4. 6.
  5. 10.
  6. 8 bytes. All object pointers are 8 bytes here, whatever they point to.
  7. It prints (1, [2, 3, 4]). The tuple is immutable, but the list inside it is not.
  8. 4. Three characters plus the terminating '\0'.
  9. The first. The block holding 5 loses its only pointer.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. Which expression gives the value 25 through the pointer p?

    int x = 25;
    int *p = &x;
    1. &p
    2. p
    3. &x
    4. *p
    Show answer

    Answer: D. *p

    &x is the address of x, and p stores that address. The dereference operator * follows the address to the value stored there, so *p is 25. p and &x are both the address, and &p is the address of the pointer variable itself.

  2. On a system where int is 4 bytes, p is an int* holding address 2000. What address does p + 1 hold?

    1. 2001
    2. 2008
    3. 2000
    4. 2004
    Show answer

    Answer: D. 2004

    Pointer arithmetic is scaled by the size of the pointed-to type. p + 1 means 'the next int', which is sizeof(int) = 4 bytes further on: 2000 + 4 = 2004. A char* would move by 1 byte.

  3. For an array arr and an int i, which expression is always equivalent to arr[i] in C and C++?

    1. *arr + i
    2. *(arr + i)
    3. &arr + i
    4. *(&arr[i] + 1)
    Show answer

    Answer: B. *(arr + i)

    The language defines arr[i] as *(arr + i): the array decays to a pointer to its first element, which is moved i elements forward and then dereferenced. *arr + i adds i to the first value instead, and *(&arr[i] + 1) is arr[i + 1].

  4. Which statement about p and q is correct?

    int a = 1, b = 2;
    const int *p = &a;   // declaration P
    int *const q = &a;   // declaration Q
    1. Both forbid changing a through them
    2. p cannot be re-pointed, and q cannot change the value
    3. *p = 5 is an error but p = &b is allowed; *q = 5 is allowed but q = &b is an error
    4. Both can be re-pointed, but neither can change the value
    Show answer

    Answer: C. *p = 5 is an error but p = &b is allowed; *q = 5 is allowed but q = &b is an error

    Read the declarations right to left. const int *p is a pointer to a constant int: the value is read-only through p, but p itself can move. int *const q is a constant pointer to an int: q is fixed to a, but *q can change a. const int *const r would forbid both.

  5. What does this Python code print?

    a = [1, 2, 3]
    b = a
    b.append(4)
    print(a, a is b)
    1. [1, 2, 3] False
    2. [1, 2, 3, 4] False
    3. [1, 2, 3, 4] True
    4. [1, 2, 3] True
    Show answer

    Answer: C. [1, 2, 3, 4] True

    Assignment in Python binds a new name to the same object; it never copies. a and b refer to one list, so appending through b is visible through a, and a is b is True. Use a.copy() or a[:] to get a separate list.

  6. On a 64-bit system with 4-byte int and 8-byte pointers, what does f print when given a 10-element int array?

    void f(int a[10]) {
        printf("%zu\n", sizeof(a));
    }
    1. 40, the size of 10 ints
    2. 8, because the parameter a is really an int*
    3. 10, the number of elements
    4. 4, the size of one int
    Show answer

    Answer: B. 8, because the parameter a is really an int*

    An array parameter is adjusted to a pointer, so int a[10] in a parameter list means int *a; the 10 is ignored. sizeof(a) is the size of a pointer, 8 bytes here. This is why C functions take the array length as a separate argument. GCC even warns about it.

  7. What does this C program print?

    #include <stdio.h>
    int main(void) {
        int arr[] = {7, 14, 21, 28, 35};
        int *p = arr + 1;
        printf("%d %d %d\n", *p, *(p + 2), p[-1]);
        return 0;
    }
    1. 7 21 0
    2. 14 16 13
    3. 14 21 7
    4. 14 28 7
    Show answer

    Answer: D. 14 28 7

    p points at arr[1] = 14. *(p + 2) is two elements further on, arr[3] = 28. p[-1] means *(p − 1), which is arr[0] = 7. A negative index is fine while it stays inside the array.

  8. double d[6] starts at address 0x3000, with an 8-byte double. What are the addresses &d[4] and (char*)d + 4?

    1. 0x3020 and 0x3004
    2. 0x3004 and 0x3004
    3. 0x3020 and 0x3020
    4. 0x3010 and 0x3004
    Show answer

    Answer: A. 0x3020 and 0x3004

    &d[4] is 4 elements of 8 bytes past the start: 0x3000 + 32 = 0x3020 (32 is 0x20 in hex). Casting to char* changes the scale to 1 byte, so (char*)d + 4 is 0x3004. Remember to convert the offset to hex.

  9. With 4-byte ints, what does the program print?

    int arr[8] = {0};
    int *p = &arr[1];
    int *q = &arr[6];
    printf("%td\n", q - p);
    1. 20
    2. 6
    3. 24
    4. 5
    Show answer

    Answer: D. 5

    Subtracting two pointers into the same array gives the distance in elements, not bytes: 6 − 1 = 5. The byte distance would be 5 × 4 = 20. %td is the printf format for ptrdiff_t.

  10. What does this C++ program print?

    #include <iostream>
    using namespace std;
    void update(int *a, int b) {
        *a += b;
        b *= 2;
    }
    int main() {
        int x = 4, y = 3;
        update(&x, y);
        update(&y, x);
        cout << x << " " << y;
    }
    1. 7 6
    2. 7 10
    3. 4 3
    4. 14 10
    Show answer

    Answer: B. 7 10

    a is a pointer, so *a += b changes the caller's variable; b is a copy, so doubling it has no effect outside. First call: x = 4 + 3 = 7. Second call: y = 3 + 7 = 10, using the updated x. Output: 7 10.

FAQs

Why does p + 1 skip 4 bytes instead of 1?

Because pointer arithmetic works in elements, not bytes. The compiler knows p points to an int, so p + 1 means "the next int", which is sizeof(int) bytes further. To step one byte at a time, convert to char* (or unsigned char*), whose element size is exactly 1.

Is an array the same as a pointer?

No, but an array name converts (decays) to a pointer to its first element in most expressions. The differences show with sizeof (whole array versus one pointer) and &arr (address of the whole array, a different type). You also cannot assign to an array name, while a pointer can be re-pointed.

Does Java have pointers?

Java has references, which are pointers without arithmetic. You cannot take an address, add to it or free memory manually; the garbage collector reclaims unreachable objects. NullPointerException is the Java equivalent of dereferencing a null pointer. Local reference variables live on the stack and the objects they refer to live on the heap, much like a C++ pointer to a new object.

What is the difference between a dangling pointer and a memory leak?

A dangling pointer still holds an address whose memory has been freed or gone out of scope, so using it is undefined behaviour. A leak is the opposite: the memory is still allocated, but no pointer to it remains, so it can never be freed. Smart pointers such as std::unique_ptr prevent both.

Next step

Go back to the official Memory Management topic, then practise class hierarchies in OOP code questions. See all topics on the Programming MCQs hub, or try an NSCT mock test.