skip to content

In a C++ image filter, why is computing a pointer one element before a pixel buffer undefined even if it is never dereferenced?

level: seniorimportance: should knowfreq 42%

answer

  1. arithmetic is defined per array
  2. valid range: element 0 to one past the end
  3. forming the pointer is the act
  4. constant evaluation diagnoses it

basics

~20 s

C++ defines pointer arithmetic only within one array: from its first element to one past the last. Computing row - 1 leaves that range, so the expression itself has undefined behaviour, whether or not anything reads through the result.

solid answer

~50 s

The standard says `P + J` and `P - J` are defined only when the result points to element `0..n` of the same array object, where `n` - one past the end - may be formed and compared but not dereferenced; anything else is undefined, at the moment of computation. A non-array object counts as a one-element array. So in a 3-tap blur, `const int* left = p - 1;` is undefined at the first pixel even if the code then picks `*p` instead. It "works" on flat-memory hardware, but the compiler may assume it never happens - and constant evaluation rejects it outright with `cannot refer to element -1 of array`. Fix it by computing the neighbour index with a clamp before forming the pointer, and use `std::less` when you must order pointers from different arrays.

code

cpp · 20 lines
cpp
#include <cstddef>
#include <cstdio>

// Horizontal 3-tap blur sum; edge pixels reuse themselves as the missing neighbour.
constexpr int blur_row_sum(const int* row, std::size_t width) {
    int total = 0;
    const int* const end = row + width;             // one past the end: may be formed
    for (const int* p = row; p != end; ++p) {
        // Broken form: const int* left = p - 1;    // undefined when p == row
        const int* left  = (p == row) ? p : p - 1;
        const int* right = (p + 1 == end) ? p : p + 1;
        total += *left + *p + *right;
    }
    return total;
}

constexpr int pixels[4] = {1, 2, 3, 4};
static_assert(blur_row_sum(pixels, 4) == 30);       // constant evaluation rejects UB

int main() { std::printf("%d\n", blur_row_sum(pixels, 4)); }

go deeper

for a junior

Recall that pointer arithmetic stays inside one array, from the first element to one past the last, and that one-past-the-end may be compared but not dereferenced.

for a middle

Explain that forming an out-of-range pointer is itself undefined, how a non-array object counts as a one-element array, and why std::less orders unrelated pointers.

for a senior

Diagnose edge-of-buffer neighbour reads in filters, clamp indices before forming pointers, and prove the arithmetic with constexpr tests, since sanitizers catch it only in narrow cases.

for a principal

Push buffer-walking code toward spans and index arithmetic with bounds stated once, so reviews stop depending on spotting a stray p - 1.

## The rule in the standard C++ does not define pointer arithmetic as "add bytes to an address". It defines it in terms of **array elements**. When an integer `J` is added to or subtracted from a pointer `P`: - if `P` points to element `i` of an array `x` with `n` elements, `P + J` points to element `i + j` **only if** `0 <= i + j <= n`, and `P - J` likewise needs `0 <= i - j <= n`; - element `n` is the **hypothetical one-past-the-end element**: you may form that pointer, compare it and subtract it, but not dereference it; - an object that is not an array element is treated as a one-element array, so `&x + 1` is fine and `&x + 2` is not; - otherwise, "the behavior is undefined". The undefined behaviour attaches to **computing** the pointer, not to using it. `row - 1` at the start of a buffer is already outside the defined range, so a later `if` that avoids reading through it does not rescue the program. Subtraction and comparison follow the same discipline. `P - Q` is defined only when both point into the same array (the result is a `std::ptrdiff_t`). Relational operators `<`, `>` on pointers into unrelated objects give an **unspecified** result, while `std::less<T*>` is required to yield an implementation-defined strict total order over pointers, which is why it is the comparator for sets and maps keyed by pointer. ## Why "it works on my machine" is not evidence On a flat address space the subtraction is just an address minus four, and nothing visibly breaks. The rule exists for two reasons: 1. **Portability of the abstract machine.** On segmented or capability-checked memory an out-of-range address may not be representable or may lose its validity, and an object at the bottom of an address range has no representable "before". 2. **Optimizer freedom.** Because the result of out-of-range arithmetic is undefined, compilers may assume that `p - 1` and `p + k` stay inside the object, and reason about loops, aliasing and comparisons from that assumption. How optimizers exploit such assumptions is covered with optimizer assumptions; here the point is that the source code made a promise it broke. ## Tools: what catches it and what does not | Tool | Catches `row - 1`? | Why | |---|---|---| | constant evaluation (`constexpr`, `static_assert`) | yes | the evaluator tracks array bounds and stops with `cannot refer to element -1 of array of 4 elements in a constant expression` | | `-fsanitize=address` | no | ASan checks memory **accesses**; an unused out-of-range pointer touches nothing | | `-Warray-bounds-pointer-arithmetic` (Clang, not in `-Wall`) | only `a - 1` written on the array | it sees a constant offset from the array itself, not `p - 1` through a loop variable | | `-fsanitize=undefined` (UBSan) | only `a - 1` written on the array | its bounds check knows the array type in `a - 1`; for `p - 1` through a pointer it reported nothing in a test with Apple clang, and `pointer-overflow` is documented for arithmetic that **overflows** or involves a null pointer | Running the filter's edge logic through a `constexpr` test is therefore one of the few mechanical ways to prove the arithmetic stays in range; the material on sanitizers covers what they do catch. ## Writing the image filter correctly The safe pattern is to decide on the **index** first and form only pointers that are in range: - Clamp the neighbour: `const int* left = (p == row) ? p : p - 1;` - the subtraction is evaluated only when `p` is past element 0. - Use `end = row + width` freely: one-past-the-end is legal to form and to compare with `!=`. - Never step past `end` "to check": a loop that computes `p + 2` near the end is undefined before the check runs. - For a 2-D image stored row-major in one allocation, compute `y * stride + x` as an index into that single array; jumping from row to row with arithmetic is fine because it is all one array. - Prefer `std::span` and `subspan` for regions; they make the bounds explicit. ## Interview phrasing A strong answer states the rule ("arithmetic is defined from the first element to one past the end of the same array"), names the consequence ("forming the pointer is the undefined act"), gives the exception for one-past-the-end, mentions the unspecified result of comparing unrelated pointers with `<` and the `std::less` alternative, and admits that sanitizers catch it only in narrow cases while constant evaluation catches it reliably. Dereferencing out of bounds - the bug that crashes - is a different, louder failure covered with classic sources of undefined behaviour.

  • Is comparing two pointers into different arrays with < undefined behaviour in C++?
    No - it is unspecified, not undefined. The relational operators on pointers to unrelated objects produce an unspecified result, so the program is valid but the answer cannot be relied on. `std::less<T*>` and the other comparison function objects are required to give an implementation-defined strict total order over pointers, which is why `std::set<T*>` and `std::map<T*, V>` are well-defined.
  • Why is it legal to compute row + width but not row + width + 1?
    The standard treats one past the last element as a hypothetical element `n`, so `row + width` points to it and may be formed, compared and subtracted, which is what makes `for (p = row; p != end; ++p)` loops work. It may not be dereferenced. `row + width + 1` would point to element `n + 1`, outside the defined range, so computing it is undefined.

  • Out-of-range pointers are fine as long as you never dereference them
  • A pointer one past the end of an array cannot even be formed
  • AddressSanitizer reports any pointer that leaves its array
  • Pointer arithmetic is just integer arithmetic on addresses
  • Comparing pointers into different arrays with < is always undefined behaviour