Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Lvalue reference to an array: `(&items)[3]`

`int* (&items)[3]` refers to the entire array of three pointers. The bound stays part of the type, and a cleanup guard can update the original pointer slots.

Version: ISO C++23; implementation support may vary

Syntax

T (&items)[3] = array;
T* (&items)[3] = pointerArray;

In int* (&items)[3], the lvalue reference to array declarator (&items)[3] groups &items in parentheses: & declares the reference, [3] gives the bound, and int* is the element type. The reference binds directly to an existing mutable int*[3] array, with no copy or array-to-pointer conversion. It cannot be rebound. Assigning to items[0] changes the first slot of the original array.

int* items[3] declares an array of pointers, but as a function parameter it is adjusted to int** and does not enforce the bound. int* (*items)[3] is a pointer to the whole array, not a reference. Without parentheses, int* &items[3] would declare an array of references, which is ill-formed. The example's reference parameter rejects a bare int** and an array of four pointers.

AllocationGuard binds to pointers, whose slots start null. In the loop, int*& p aliases each pointer slot rather than copying its value. The array must outlive the guard: the destructor uses it on normal block exit and during stack unwinding to an exception handler, so the guard can clean up allocations that succeeded before a failure.

delete pointers[0] does not reset the slot. Immediately assigning pointers[0] = nullptr lets the guard see a null pointer instead of attempting a second deletion, which would risk undefined behavior. Deleting a null pointer is valid. Use delete here because each new int creates one object; use delete[] for allocations made with new[]. Each non-null slot must designate a distinct object to be deleted. In production, prefer std::unique_ptr to represent exclusive ownership.

Examples

#include <iostream>
#include <new>

class AllocationGuard {
    int* (&items)[3];

public:
    explicit AllocationGuard(int* (&array)[3]) noexcept : items(array) {}
    AllocationGuard(const AllocationGuard&) = delete;
    AllocationGuard& operator=(const AllocationGuard&) = delete;

    ~AllocationGuard() noexcept {
        for (int*& p : items) {
            delete p;
            p = nullptr;
        }
    }
};

int main() {
    int* pointers[3]{};
    try {
        AllocationGuard guard{pointers};
        pointers[0] = new int{10};
        pointers[1] = new int{20};
        pointers[2] = new int{30};

        delete pointers[0];
        pointers[0] = nullptr;
    } catch (const std::bad_alloc&) {
        return 1;
    }

    const bool allNull = pointers[0] == nullptr &&
                         pointers[1] == nullptr &&
                         pointers[2] == nullptr;
    std::cout << std::boolalpha << "All slots are null: " << allNull << '\n';
}

Result

All slots are null: true

The expected line is printed after the guard's destructor runs. The first slot was reset explicitly; the guard deletes the other two objects and clears their slots in the same array.

Related lessons

  • C++ · Lesson 20 · Advanced · Version: 3

Sources