Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C++  Module 5 – Arrays, vectors and aggregate types

2D grids in C++: flat storage and std::mdspan

Store a grid as a vector of vectors or as one flat vector, index it with C++23 std::mdspan and m[r, c], and see why row-by-row loops ran over ten times faster.

  • Intermediate
  • 20 minutes
  • Examples run with Clang 21.0.0, GCC 16.2.0 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 20 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Store a grid in a vector of vectors and in flat storage
  • Use std::mdspan to index flat storage as a 2D grid
  • Avoid the comma trap of C++23 subscripts
  • Traverse grids in a cache-friendly order
  • Check neighbours without reading outside the grid

Before you start

What you’ll learn · Before you start

What you will learn

  • Store a grid in a vector of vectors and in flat storage
  • Use std::mdspan to index flat storage as a 2D grid
  • Avoid the comma trap of C++23 subscripts
  • Traverse grids in a cache-friendly order
  • Check neighbours without reading outside the grid

Before you start

On this page

On an Apple M5 Pro, adding up a 64 MiB grid of ints column by column took more than ten times as long as row by row, with both Apple clang and GCC 16. C++ has no built-in grid type, so a 2D grid, a game board, an image or a matrix, is stored in one of three ways. A vector of vectors, std::vector<std::vector<int>>, is the easiest to write: g[r][c] reads row r, column c. But every row is a separate allocation on the heap. Flat storage, one std::vector<int> of rows * cols elements with row r starting at index r * cols, keeps the whole grid in one contiguous block, which is the layout C arrays, image formats and maths libraries use. And std::mdspan (C++23) is a view over flat storage that does the index arithmetic for you, so you write m[r, c] and still get one block of memory.

For anything large or speed-sensitive, use flat storage with an mdspan, and visit it row by row. Two traps follow: the comma of C++23 subscripts, and neighbours read from outside the grid.

A grid can be nested vectors, one flat vector or an mdspan view

Here is one 3 × 4 grid, with 10 × row + column in each cell, stored as a vector of vectors and as one flat vector:

A 3 by 4 grid as a vector of vectors, with each row allocated separately, and as one flat vector whose rows follow each other.vector of vectors: 4 allocationsflat vector: 1 allocationouter vectorthree separate buffers[0]0[1]1[2]2[3]3[4]10[5]11[6]12[7]13[8]20[9]21[10]22[11]23row 0row 1row 20 1 2 310 11 12 1320 21 22 23each row pointsto its own buffersame grid: (r, c)at index r * 4 + c

The same grid as a vector of vectors and as flat storage

Text description of the diagram

The diagram shows the lesson's 3 x 4 grid, in which the value 10 × row + column is stored at row r, column c.

  1. As a std::vector<std::vector<int>>, the grid takes 4 allocations: the outer vector holds 3 row objects, and each row object points to its own buffer of 4 ints (0 1 2 3, then 10 11 12 13, then 20 21 22 23). The three buffers can be anywhere on the heap.
  2. As one flat std::vector<int>, the grid takes 1 allocation of 12 ints. Row 0 fills indexes 0 to 3, row 1 indexes 4 to 7 and row 2 indexes 8 to 11, so the element at row r, column c is at index r × 4 + c: row 1, column 2 is index 6, which holds 12.

Reading a row means reading neighbouring memory in both layouts. Going down a column means jumping 4 ints ahead in the flat vector, and to a different buffer in the vector of vectors.

three_ways.cpp C++ · three_ways.cpp
// The same 3 x 4 grid stored three ways.
#include <cstddef>
#include <mdspan>
#include <print>
#include <vector>

int main() {
    const std::size_t rows = 3, cols = 4;

    // 1. A vector of vectors: one allocation for the outer vector and one more for each row.
    std::vector<std::vector<int>> nested(rows, std::vector<int>(cols));
    // 2. One flat vector: the element in row r, column c is at index r * cols + c.
    std::vector<int> flat(rows * cols);
    // 3. A std::mdspan (C++23) over the flat vector's elements: it does the index arithmetic.
    std::mdspan grid(flat.data(), rows, cols);

    for (std::size_t r = 0; r < rows; ++r)
        for (std::size_t c = 0; c < cols; ++c) {
            int value = static_cast<int>(10 * r + c);   // 10 * row + column, so 21 is row 2, column 1
            nested[r][c] = value;
            flat[r * cols + c] = value;
        }

    std::println("nested: {}", nested);
    std::println("flat:   {}", flat);
    std::print("grid:   ");
    for (std::size_t r = 0; r < grid.extent(0); ++r) {
        std::print("{}[", r ? " " : "");
        for (std::size_t c = 0; c < grid.extent(1); ++c) std::print("{}{}", c ? ", " : "", grid[r, c]);
        std::print("]");
    }
    std::println("");

    grid[1, 2] = 99;                                  // writes through the view, into flat
    std::println("after grid[1, 2] = 99: flat[{}] = {}, nested[1][2] = {}", 1 * cols + 2, flat[1 * cols + 2], nested[1][2]);
    std::println("grid: {} rows, {} columns, {} elements", grid.extent(0), grid.extent(1), grid.size());
}
One grid, three ways, two compilers Python · three_ways_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra three_ways.cpp -o three_ways
./three_ways
g++-16 -std=c++23 -Wall -Wextra three_ways.cpp -o three_ways-gcc
./three_ways-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra three_ways.cpp -o three_ways
$ ./three_ways
nested: [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]
flat:   [0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23]
grid:   [0, 1, 2, 3] [10, 11, 12, 13] [20, 21, 22, 23]
after grid[1, 2] = 99: flat[6] = 99, nested[1][2] = 12
grid: 3 rows, 4 columns, 12 elements
$ g++-16 -std=c++23 -Wall -Wextra three_ways.cpp -o three_ways-gcc
$ ./three_ways-gcc
nested: [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]
flat:   [0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23]
grid:   [0, 1, 2, 3] [10, 11, 12, 13] [20, 21, 22, 23]
after grid[1, 2] = 99: flat[6] = 99, nested[1][2] = 12
grid: 3 rows, 4 columns, 12 elements

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 three_ways_session.py

  • The vector of vectors prints as rows inside rows. nested(rows, std::vector<int>(cols)) copies the row vector three times, which makes 4 allocations: the outer vector and one per row. Rows may even differ in length, which is the one thing this layout does that the others cannot.
  • The flat vector prints as one list in which the rows simply follow each other. The element at row r, column c is flat[r * cols + c]: row 1, column 2 is index 6.
  • The mdspan grid copies nothing. std::mdspan grid(flat.data(), rows, cols) remembers a pointer to the first element and the two extent: The size of one dimension of a std::mdspan, such as its number of rows. extent(0) and extent(1) give the two of a grid; std::extents fixes them while compiling, and std::dextents keeps them as values chosen while the program runs., the sizes of its dimensions, and turns grid[r, c] into flat[r * cols + c]. Writing grid[1, 2] = 99 changed flat[6], while the separate nested copy kept 12.

Step through the program and watch where each value lives, and where the write through the view lands:

Interactive · step by step One 3 × 4 grid, stored three ways Step through three_ways.cpp. The left column holds the two vectors, the right one the mdspan and the write through it; an arrow shows what the view points into.
One 3 × 4 grid, stored three ways, step 6 of 6. Showing vector of vectors (nested[1][2] still 12), flat vector of 12 ints (flat[6] is now 99), std::mdspan grid (3 × 4, 12 elements), grid[1, 2] = 99. In focus: std::mdspan grid.viewsgoes throughvector of vectorsnested[1][2] still 12flat vector of 12 intsflat[6] is now 99std::mdspan grid3 × 4, 12 elementsgrid[1, 2] = 99index 1 × 4 + 2 = 6

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Rows inside rows. nested(rows, std::vector<int>(cols)) makes the outer vector and copies the row vector into it three times. The finished grid holds 4 allocations, and each row's 4 ints can sit anywhere on the heap.
  2. One block. flat(rows * cols) is one allocation of 12 ints. Row r starts at index r × 4, so row 1 fills indexes 4 to 7.
  3. A view, not a copy. std::mdspan grid(flat.data(), rows, cols) remembers the pointer to flat's first int and the two extents, 3 and 4. It copies no element.
  4. The same values. Both loops store 10 × row + column, so 21 is row 2, column 1: nested[2][1], and flat[2 × 4 + 1], which is flat[9]. All three printed the same twelve values.
  5. Writing through the view. grid[1, 2] = 99 turns the two indexes into 1 × 4 + 2 = 6 and writes into flat. The program printed flat[6] = 99, while nested[1][2], a separate copy, still held 12.
  6. The view's size. extent(0) and extent(1) give the rows and the columns, and size() their product: the program printed grid: 3 rows, 4 columns, 12 elements.

The view never held a value of its own: its one write went into flat[6], and only the separate nested copy kept the old 12.

Key idea

Flat storage puts row r, column c at index r * cols + c in one allocation; an mdspan does that arithmetic for you, and a vector of vectors pays one allocation per row.

Checkpoint

Check: Three ways to store a grid

  1. Question 1 of 2 A grid with 4 columns is stored row by row in a flat vector. Which cell is at index 9?

    Choose one answer.

    A hint

    How many whole rows of 4 fit before index 9?

    Show the answer: Question 1 of 2

    Answer: Row 2, column 1

    Divide by the number of columns: 9 / 4 is 2 with 1 left over, so row 2, column 1. The formula run backwards is r = i / cols and c = i % cols.

  2. Question 2 of 2 std::vector<std::vector<int>> g(5, std::vector<int>(8)); How many heap allocations does the finished grid hold?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 6 allocations

    One for the outer vector's five row objects and one for each row's 8 ints: 1 + 5 = 6, just as the 3-row grid of three_ways.cpp took 4.

std::mdspan turns two indexes into one position in flat memory

std::mdspan comes from the header <mdspan>, and both compilers used here ship it. It is a view, like std::span in one dimension: it does not own the elements, so it must not outlive the vector it looks into, and it dangles just like a pointer if that vector reallocates. Writing m[r, c] with two indexes inside one pair of brackets is new in C++23 too: a class may now declare operator[] with several parameters.

An mdspan’s type says how much it knows while compiling, and how the indexes map to memory:

layouts.cpp C++ · layouts.cpp
// One buffer of 6 ints, three mdspan views: sizes fixed at compile time, row-major and column-major.
#include <cstddef>
#include <mdspan>
#include <print>

int main() {
    int buffer[6]{1, 2, 3, 4, 5, 6};

    // Both sizes known while compiling: 2 rows, 3 columns. Row-major (layout_right) is the default.
    std::mdspan<int, std::extents<std::size_t, 2, 3>> by_rows(buffer);
    // The same 6 ints read column after column (layout_left), the order Fortran and MATLAB use.
    std::mdspan<int, std::extents<std::size_t, 2, 3>, std::layout_left> by_columns(buffer);

    for (std::size_t r = 0; r < 2; ++r) {
        std::println("row {}: by_rows {} {} {}   by_columns {} {} {}", r, by_rows[r, 0], by_rows[r, 1], by_rows[r, 2],
                     by_columns[r, 0], by_columns[r, 1], by_columns[r, 2]);
    }
    std::println("by_rows[1, 0] is buffer[{}]; by_columns[1, 0] is buffer[{}]", by_rows.mapping()(1, 0), by_columns.mapping()(1, 0));
    std::println("static extents cost no memory: sizeof(by_rows) = {}", sizeof by_rows);

    std::mdspan<int, std::dextents<std::size_t, 2>> sized_at_run_time(buffer, 3, 2);   // 3 x 2 this time
    std::println("3 x 2 view: [2, 1] = {}, sizeof = {}", sized_at_run_time[2, 1], sizeof sized_at_run_time);
}
Fixed and run-time extents, row-major and column-major Python · layouts_session.py
from session import run

run("""
clang++ -std=c++23 -Wall -Wextra layouts.cpp -o layouts
./layouts
g++-16 -std=c++23 -Wall -Wextra layouts.cpp -o layouts-gcc
./layouts-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra layouts.cpp -o layouts
$ ./layouts
row 0: by_rows 1 2 3   by_columns 1 3 5
row 1: by_rows 4 5 6   by_columns 2 4 6
by_rows[1, 0] is buffer[3]; by_columns[1, 0] is buffer[1]
static extents cost no memory: sizeof(by_rows) = 8
3 x 2 view: [2, 1] = 6, sizeof = 24
$ g++-16 -std=c++23 -Wall -Wextra layouts.cpp -o layouts-gcc
$ ./layouts-gcc
row 0: by_rows 1 2 3   by_columns 1 3 5
row 1: by_rows 4 5 6   by_columns 2 4 6
by_rows[1, 0] is buffer[3]; by_columns[1, 0] is buffer[1]
static extents cost no memory: sizeof(by_rows) = 8
3 x 2 view: [2, 1] = 6, sizeof = 24

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 layouts_session.py

  • std::extents<std::size_t, 2, 3> fixes both sizes while compiling. Such an mdspan is just a pointer, 8 bytes. std::dextents<std::size_t, 2> keeps two sizes chosen while the program runs, and takes 24 bytes.
  • The default layout, std::layout_right, is row-major order: Storing a grid row after row, so that the last index changes fastest: row r, column c of a grid with C columns is at r × C + c. C arrays of arrays use it, and so does std::mdspan's default layout, layout_right.: the last index changes fastest, as in a C array of arrays. std::layout_left is column-major order: Storing a grid column after column, so that the first index changes fastest: row r, column c of a grid with R rows is at r + c × R. Fortran and MATLAB store matrices this way, and std::mdspan reads it with layout_left., the order of Fortran and MATLAB, so the same six ints read 1 3 5 along row 0 instead of 1 2 3.
  • mapping()(r, c) shows where an element lives: by_rows[1, 0] is buffer[3], by_columns[1, 0] is buffer[1]. That is how you pass data between C++ and a column-major library without copying it.
  • extent(0), extent(1) and size() give the number of rows, columns and elements. m[r, c] does not check its indexes, any more than v[i] does.

Step through the three views and the arithmetic each one does with its two indexes:

Interactive · stage by stage Where [r, c] lands in the six ints of layouts.cpp Step through the three views. Each stage is one mapping from two indexes to a position in buffer, and its sub-label shows the arithmetic.
Where [r, c] lands in the six ints of layouts.cpp, step 5 of 5. Showing int buffer[6], by_rows: layout_right ([1, 0] is buffer[3]), by_columns: layout_left ([1, 0] is buffer[1]), dextents: 3 × 2 ([2, 1] is buffer[5], 24 bytes). In focus: dextents: 3 × 2.int buffer[6]1 2 3 4 5 6 at 0 to 5by_rows: layout_right[1, 0] is buffer[3]by_columns: layout_left[1, 0] is buffer[1]dextents: 3 × 2[2, 1] is buffer[5], 24 bytes

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. Six ints in a row. All three views look into the same buffer of six ints, 1 to 6 at positions 0 to 5. None of them copies anything.
  2. Row-major, the default. layout_right lets the last index change fastest: [r, c] is at r × 3 + c. [1, 0] is 1 × 3 + 0 = 3, buffer[3], and row 0 reads 1 2 3, as the program printed.
  3. Column-major. layout_left lets the first index change fastest: [r, c] is at r + c × 2. [1, 0] is 1 + 0 × 2 = 1, buffer[1], and row 0 reads 1 3 5, every second int.
  4. Sizes fixed while compiling. Both of these views have both extents in their type, std::extents<std::size_t, 2, 3>, so each one is only a pointer: sizeof(by_rows) = 8.
  5. Sizes chosen while running. std::dextents<std::size_t, 2> keeps two sizes, here 3 rows and 2 columns, beside the pointer: 24 bytes. [2, 1] is 2 × 2 + 1 = 5, and buffer[5] holds 6, as printed.

The same six ints gave three different grids: only the mapping changed, never the memory.

Key idea

An mdspan is a pointer and a mapping: layout_right puts [r, c] at r × columns + c, layout_left at r + c × rows, and neither copies or checks anything.

Checkpoint

Check: Extents and layouts of std::mdspan

  1. Question 1 of 2 A 2 × 3 view with layout_left, like by_columns. At which position of the buffer is [1, 2]?

    Type a number.

    A hint

    In layout_left, [r, c] is at r + c × rows.

    Show the answer: Question 1 of 2

    Answer: 5

    1 + 2 × 2 = 5, so buffer[5], which holds 6. by_columns printed row 1 as 2 4 6, and its last element is the last int of the buffer.

  2. Question 2 of 2 How many bytes did the program print for sizeof(by_rows), an mdspan whose two extents are both fixed while compiling?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 8 bytes

    With std::extents<std::size_t, 2, 3> both sizes are part of the type, so the object holds only its pointer, 8 bytes. The view with dextents kept the two sizes as well: 24 bytes.

In C++23, grid[1, 2] calls an operator[] with two arguments

The multidimensional subscript changed the meaning of a comma between brackets. Before C++20, a[1, 2] used the comma operator, which throws away its left side, so it meant a[2]; C++20 deprecated that. In C++23 it is a call of operator[] with two arguments, which std::vector does not have:

comma_subscript.cpp C++ · comma_subscript.cpp
// grid[1, 2] on a vector of vectors: meant as "row 1, column 2".
#include <print>
#include <vector>

int main() {
    std::vector<std::vector<int>> grid{{1, 2, 3}, {4, 5, 6}};
    std::println("{}", grid[1, 2]);
}
Clang refuses it; GCC warns and reads grid[2] Python · comma_session.py
from session import run

# C++23 reads grid[1, 2] as a call of operator[] with two arguments, which std::vector does not have.
run("""
clang++ -std=c++23 comma_subscript.cpp -o comma_subscript
g++-16 -std=c++23 comma_subscript.cpp -o comma_subscript-gcc
./comma_subscript-gcc
""", max_lines=6)

Output

$ clang++ -std=c++23 comma_subscript.cpp -o comma_subscript
comma_subscript.cpp:7:28: error: no viable overloaded operator[] for type 'std::vector<std::vector<int>>'
    7 |     std::println("{}", grid[1, 2]);
      |                        ~~~~^~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__vector/vector.h:413:83: note: candidate function not viable: requires single argument '__n', but 2 arguments were provided
  413 |   [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT {
      |                                                                                   ^          ~~~~~~~~~~~~~
[4 more lines]
[exit status 1]
$ g++-16 -std=c++23 comma_subscript.cpp -o comma_subscript-gcc
comma_subscript.cpp: In function 'int main()':
comma_subscript.cpp:7:28: warning: top-level comma expression in array subscript changed meaning in C++23 [-Wcomma-subscript]
    7 |     std::println("{}", grid[1, 2]);
      |                            ^
$ ./comma_subscript-gcc
/opt/homebrew/Cellar/gcc/16.2.0/include/c++/16/bits/stl_vector.h:1253: constexpr std::vector< <template-parameter-1-1>, <template-parameter-1-2> >::reference std::vector< <template-parameter-1-1>, <template-parameter-1-2> >::operator[](size_type) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; reference = std::vector<int>&; size_type = long unsigned int]: Assertion '__n < this->size()' failed.
[stopped by SIGABRT: a shell reports exit status 134]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 comma_session.py

Clang rejects the line. GCC 16 warns that the meaning changed and, as its manual says it does when no two-argument operator[] exists, falls back to the old comma operator: it read grid[2], a row that does not exist, and libstdc++’s debug assertion stopped the program. For a vector of vectors write grid[1][2]; grid[1, 2] is only for types such as std::mdspan that define it.

The same subscript has meant different things in three editions of C++. Walk through them, then through the two compilers of this lesson:

Timeline · 5 events in order What grid[1, 2] has meant in each edition of C++ Walk through the editions in order, then the two compilers of this lesson, and watch the same subscript change its meaning.
  1. C++17 and before

    The comma operator

    Inside the brackets, 1, 2 is one expression: the comma operator evaluates 1, throws it away and gives 2. So a[1, 2] means a[2], and grid[1, 2] on a vector of vectors names the whole of row 2.

  2. C++20

    Deprecated in subscripts deprecated

    Paper P1161R3 deprecates a comma operator written directly inside subscript brackets; a[(1, 2)] in parentheses keeps the old meaning. The deprecation cleared the way for a new meaning.

  3. C++23

    Two arguments to operator[] new syntax

    Paper P2128R6 lets a class declare operator[] with several parameters, so grid[1, 2] calls operator[](1, 2). std::mdspan declares one; std::vector does not.

  4. Clang

    An error for std::vector

    With -std=c++23, Clang refused grid[1, 2] on a vector of vectors: no viable overloaded operator[], since vector's operator[] takes a single argument.

  5. GCC 16

    A warning, then the old meaning

    GCC warned that the comma changed meaning in C++23 and, finding no two-argument operator[], fell back to the comma operator: it read grid[2], a row that does not exist, and its debug assertion stopped the program.

The timeline has 5 events, from C++17 and before to GCC 16.

Only a type that declares a two-argument operator[], such as std::mdspan or the Matrix of this lesson’s exercise, gives grid[1, 2] its new meaning.

Checkpoint

Check: The comma in a C++23 subscript

  1. Question 1 of 2 What did GCC 16 do with grid[1, 2] on a vector of vectors?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: It warned that the comma changed meaning, read grid[2], and its debug assertion stopped the program

    GCC's -Wcomma-subscript warning said the expression changed meaning in C++23; then the comma operator gave grid[2], one past the last row, and libstdc++'s assertion __n < this->size() stopped the run with SIGABRT.

  2. Question 2 of 2 How do you read row 1, column 2 of a std::vector<std::vector<int>> called grid?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: grid[1][2]

    A vector of vectors takes one subscript per level: grid[1][2]. Two indexes in one pair of brackets work only for types that declare a two-argument operator[], such as std::mdspan.

Visit a row-major grid row by row

Row-major storage means that the elements of one row sit next to each other, and the next row follows. A loop with the column in the inner loop reads memory in order; a loop with the row in the inner loop jumps a whole row ahead on every step. This program measures both, for a grid that fits in the processor’s caches and one that does not:

traverse.cpp C++ · traverse.cpp
// Times four ways to add up every int of an n x n grid, for a small and a large grid, and prints the fastest of
// 5 runs of each. Timings differ from run to run and from computer to computer; the ratios are what matter.
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <mdspan>
#include <print>
#include <vector>

using Clock = std::chrono::steady_clock;
volatile std::int64_t sink = 0;   // each sum is stored here, so the compiler cannot skip computing it

[[gnu::noinline]] std::int64_t flat_rows(const std::vector<int>& g, std::size_t n) {
    std::int64_t sum = 0;
    for (std::size_t r = 0; r < n; ++r)
        for (std::size_t c = 0; c < n; ++c) sum += g[r * n + c];   // the next int in memory every time
    return sum;
}

[[gnu::noinline]] std::int64_t flat_columns(const std::vector<int>& g, std::size_t n) {
    std::int64_t sum = 0;
    for (std::size_t c = 0; c < n; ++c)
        for (std::size_t r = 0; r < n; ++r) sum += g[r * n + c];   // jumps n ints ahead every time
    return sum;
}

[[gnu::noinline]] std::int64_t mdspan_rows(std::mdspan<const int, std::dextents<std::size_t, 2>> m) {
    std::int64_t sum = 0;
    for (std::size_t r = 0; r < m.extent(0); ++r)
        for (std::size_t c = 0; c < m.extent(1); ++c) sum += m[r, c];
    return sum;
}

[[gnu::noinline]] std::int64_t nested_rows(const std::vector<std::vector<int>>& g) {
    std::int64_t sum = 0;
    for (const auto& row : g)
        for (int x : row) sum += x;
    return sum;
}

template <class F>
double fastest_ms(F f) {
    double best = 1e300;
    for (int run = 0; run < 5; ++run) {
        auto start = Clock::now();
        sink = f();
        std::chrono::duration<double, std::milli> took = Clock::now() - start;
        best = std::min(best, took.count());
    }
    return best;
}

int main() {
    for (std::size_t n : {512, 4096}) {
        std::vector<int> flat(n * n);
        for (std::size_t i = 0; i < flat.size(); ++i) flat[i] = static_cast<int>(i % 10);
        std::vector<std::vector<int>> nested(n, std::vector<int>(n));
        for (std::size_t r = 0; r < n; ++r)
            for (std::size_t c = 0; c < n; ++c) nested[r][c] = flat[r * n + c];
        std::mdspan<const int, std::dextents<std::size_t, 2>> view(flat.data(), n, n);

        double rows = fastest_ms([&] { return flat_rows(flat, n); });
        double columns = fastest_ms([&] { return flat_columns(flat, n); });
        double md = fastest_ms([&] { return mdspan_rows(view); });
        double nest = fastest_ms([&] { return nested_rows(nested); });
        std::println("{} x {} ints, {} KiB:", n, n, flat.size() * sizeof(int) / 1024);
        std::println("  flat, row by row        {:8.3f} ms", rows);
        std::println("  flat, column by column  {:8.3f} ms  ({:.0f} times slower)", columns, columns / rows);
        std::println("  mdspan, row by row      {:8.3f} ms", md);
        std::println("  vector of vectors, rows {:8.3f} ms", nest);
    }
}
Four ways to add up a grid, timed Python · traverse_session.py
from session import run

# The cache line and the level-2 cache of this Mac's performance cores (Apple M5 Pro), in bytes, then both compilers
# with -O2. The milliseconds change from run to run; the ratios are what matter.
run("""
/usr/sbin/sysctl -n hw.cachelinesize hw.perflevel0.l2cachesize
clang++ -std=c++23 -O2 -Wall -Wextra traverse.cpp -o traverse
./traverse
g++-16 -std=c++23 -O2 -Wall -Wextra traverse.cpp -o traverse-gcc
./traverse-gcc
""")

Output

$ /usr/sbin/sysctl -n hw.cachelinesize hw.perflevel0.l2cachesize
128
16777216
$ clang++ -std=c++23 -O2 -Wall -Wextra traverse.cpp -o traverse
$ ./traverse
512 x 512 ints, 1024 KiB:
  flat, row by row           0.022 ms
  flat, column by column     0.623 ms  (28 times slower)
  mdspan, row by row         0.023 ms
  vector of vectors, rows    0.022 ms
4096 x 4096 ints, 65536 KiB:
  flat, row by row           1.277 ms
  flat, column by column    97.001 ms  (76 times slower)
  mdspan, row by row         1.194 ms
  vector of vectors, rows    1.293 ms
$ g++-16 -std=c++23 -O2 -Wall -Wextra traverse.cpp -o traverse-gcc
$ ./traverse-gcc
512 x 512 ints, 1024 KiB:
  flat, row by row           0.115 ms
  flat, column by column     0.337 ms  (3 times slower)
  mdspan, row by row         0.110 ms
  vector of vectors, rows    0.113 ms
4096 x 4096 ints, 65536 KiB:
  flat, row by row           6.978 ms
  flat, column by column    86.412 ms  (12 times slower)
  mdspan, row by row         6.990 ms
  vector of vectors, rows    7.051 ms

This output changes from run to run: the times depend on the computer and on what else it is doing

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 traverse_session.py

The first two lines are the processor’s cache line: The block in which memory travels into the processor's caches, 128 bytes on this course's Mac. Reading one int brings in its whole line, so the ints next to it come almost free, while a read somewhere else waits for another line., 128 bytes, and the level-2 cache of its performance cores, 16 MiB. Memory reaches the processor in whole cache lines:

  • Row by row, all 32 ints of every 128-byte line get used, and the hardware sees the pattern and fetches the next lines early.
  • Column by column, every step jumps a whole row ahead, 16 KiB in the large grid, so each read lands in a different cache line, uses 4 of its 128 bytes and has to wait for a slower level of the memory system. In the recorded run, the 64 MiB grid took more than 50 times longer this way with Clang and more than 10 times longer with GCC, and even the 1 MiB grid, which fits in the level-2 cache, was several times slower.
  • The two compilers differ because Clang turned the row loop into vector instructions that add 16 ints per step, while GCC’s -O2 build added them one at a time. Both compilers were equally slow down the columns: there, memory is the limit.
  • mdspan costs nothing: its row loop ran as fast as the hand-written r * n + c. The vector of vectors was just as fast here too, because each row is contiguous. Its costs show elsewhere: one allocation per row, 24 bytes of bookkeeping per row, and rows scattered over the heap, so stepping between rows, as a column loop or a neighbour check does, jumps to unrelated memory.

Step through both loops over the large grid and watch what each one does with a line:

Interactive · step by step What each loop order does with a 128-byte cache line Step through the two loops over the 4,096 × 4,096 grid. The left column is memory, the right one the processor; an arrow is a cache line on its way in.
What each loop order does with a 128-byte cache line, step 5 of 5. Showing one row: 4,096 ints, the grid: 64 MiB, the processor (over 50 times slower (Clang)), level-2 cache: 16 MiB. In focus: the processor.one line at a timedoes not fitone row: 4,096 ints16 KiB, side by sidethe grid: 64 MiB4 times the L2 cachethe processorover 50 times slower (Clang)level-2 cache: 16 MiBof the performance cores

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. Memory comes in lines. The first command of the session printed this Mac's cache line, 128 bytes: memory reaches the processor in whole lines, never one int at a time. A line holds 128 / 4 = 32 ints.
  2. Row by row. With the column in the inner loop, each read is the next int in memory, so one line serves 32 reads in a row, and the hardware sees the pattern and fetches the next lines early.
  3. Column by column. With the row in the inner loop, each step jumps a whole row ahead, 4,096 ints or 16 KiB. Every read lands in a different line and uses 4 of its 128 bytes.
  4. Small grid, large grid. The 1 MiB grid fits in the 16 MiB level-2 cache, and its column order was still several times slower. The 64 MiB grid is four times that cache, and its gap was wider still.
  5. The cost, measured. In the recorded run, the column order took more than 50 times as long as the row order with Clang, and more than 10 times as long with GCC: the same ints and the same sum.

Both loops read the same 16,777,216 ints and arrive at the same sum; only the order decided how many lines had to come in from memory. The three layouts side by side:

Vector of vectors, flat vector or mdspan?
Criterion Nested vectorsFlat vectormdspan view
Allocations Rows + 111 (the vector's)
Element g[r][c]g[r * cols + c]m[r, c]
Ragged rows YesNoNo
Row by row As fastFastestAs fast
Pass it as A referenceVector and widthOne small value
When to choose Small or ragged gridsC librariesC++23 grids

Checkpoint

Check: Row-by-row traversal and cache lines

  1. Question 1 of 2 A cache line on this Mac is 128 bytes. How many 4-byte ints does one line hold?

    Type a number.

    Show the answer: Question 1 of 2

    Answer: 32 ints

    128 / 4 = 32. Row by row, all 32 are used before the next line is needed; column by column, one int of each line is.

  2. Question 2 of 2 In the 4,096 × 4,096 grid of ints, how many KiB does each step of the column-by-column loop jump?

    Type a number.

    A hint

    One step moves to the next row. How many bytes is a row?

    Show the answer: Question 2 of 2

    Answer: 16 KiB

    A row is 4,096 ints × 4 bytes = 16,384 bytes, 16 KiB, so every read lands in a different cache line and uses 4 of its 128 bytes.

Check all four bounds before reading a neighbour

Grid searches, flood fills and game boards all look at the cells around a cell, and the cells on the edge have fewer neighbours. Check each bound before the read:

neighbours.cpp C++ · neighbours.cpp
// For every cell of a small map, counts how many of its four neighbours (up, down, left, right) are land, '#'.
#include <mdspan>
#include <print>
#include <string_view>
#include <vector>

using Map = std::mdspan<const char, std::dextents<int, 2>>;   // int coordinates: one step up from row 0 is -1

bool in_bounds(Map m, int r, int c) {
    return r >= 0 && r < m.extent(0) && c >= 0 && c < m.extent(1);
}

int land_around(Map m, int r, int c) {
    const int dr[]{-1, 1, 0, 0};
    const int dc[]{0, 0, -1, 1};
    int land = 0;
    for (int k = 0; k < 4; ++k) {
        int nr = r + dr[k], nc = c + dc[k];
        if (in_bounds(m, nr, nc) && m[nr, nc] == '#') ++land;   // check first, then read
    }
    return land;
}

int land_around_unchecked(Map m, int r, int c) {   // the same without in_bounds: the bug
    const int dr[]{-1, 1, 0, 0};
    const int dc[]{0, 0, -1, 1};
    int land = 0;
    for (int k = 0; k < 4; ++k)
        if (m[r + dr[k], c + dc[k]] == '#') ++land;
    return land;
}

int main() {
    const std::string_view lines[]{".#..", "##.#", "..##"};
    const int rows = 3, cols = 4;
    std::vector<char> cells;
    for (std::string_view line : lines) cells.insert(cells.end(), line.begin(), line.end());
    Map map(cells.data(), rows, cols);

    for (int r = 0; r < rows; ++r) {
        std::print("{}   ", lines[r]);
        for (int c = 0; c < cols; ++c) std::print("{}", land_around(map, r, c));
        std::println("");
    }
    std::println("corner (0, 0) without the check: {}", land_around_unchecked(map, 0, 0));
}
Checked counts, then the unchecked function under AddressSanitizer Python · neighbours_session.py
from session import run

# The checked counts, then the unchecked function under AddressSanitizer (key lines, addresses hidden).
run("""
clang++ -std=c++23 -Wall -Wextra neighbours.cpp -o neighbours
./neighbours
clang++ -std=c++23 -g -fsanitize=address neighbours.cpp -o neighbours-asan
ASAN_OPTIONS=symbolize=0 ./neighbours-asan 2>&1 | grep -E "ERROR|of size|located" | sed -E 's/==[0-9]+==//; s/0x[0-9a-f]+/0x…/g'
""")

Output

$ clang++ -std=c++23 -Wall -Wextra neighbours.cpp -o neighbours
$ ./neighbours
.#..   2111
##.#   1231
..##   1212
corner (0, 0) without the check: 2
$ clang++ -std=c++23 -g -fsanitize=address neighbours.cpp -o neighbours-asan
$ ASAN_OPTIONS=symbolize=0 ./neighbours-asan 2>&1 | grep -E "ERROR|of size|located" | sed -E 's/==[0-9]+==//; s/0x[0-9a-f]+/0x…/g'
ERROR: AddressSanitizer: heap-buffer-overflow on address 0x… at pc 0x… bp 0x… sp 0x…
READ of size 1 at 0x… thread T0
0x… is located 4 bytes before 16-byte region [0x…,0x…)
allocated by thread T0 here:

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 neighbours_session.py

The counts are right, and so, by luck, is the unchecked corner: it read two bytes outside the map and neither happened to be '#'. sanitizer: A compiler option, such as -fsanitize=address or -fsanitize=undefined, that builds extra checks into a program, so that memory errors and undefined behaviour are reported, with their source line, while it runs. More in “Finding memory bugs with AddressSanitizer and UBSan” shows what really happened: the step up from row 0 read 4 bytes before the map’s buffer, row -1 of a 4-column map. Two rules prevent this:

  • Test all four bounds, r >= 0, r < rows, c >= 0 and c < cols, before every read. Checking only that the flat index lies inside the vector is not enough: one step right from the last column lands, inside the vector, on the first cell of the next row.
  • Use signed coordinates for neighbour arithmetic, as std::dextents<int, 2> does here. With std::size_t, row 0 minus 1 wraps around to 18,446,744,073,709,551,615, so a forgotten r >= 0 test fails in a different way.

Step through the corner cell (0, 0), one neighbour at a time, then the same cell without the check:

Interactive · stage by stage The four neighbours of the corner (0, 0) Step through land_around(map, 0, 0), then its unchecked twin. Each stage is one neighbour, tried in the order of dr and dc.
The four neighbours of the corner (0, 0), step 5 of 5. Showing up: (-1, 0) (r >= 0 fails: skipped), down: (1, 0) (# : land is 1), left: (0, -1) (c >= 0 fails: skipped), right: (0, 1) (# : land is 2), unchecked: map[-1, 0] (4 bytes before the buffer). In focus: unchecked: map[-1, 0].up: (-1, 0)r >= 0 fails: skippeddown: (1, 0)# : land is 1left: (0, -1)c >= 0 fails: skippedright: (0, 1)# : land is 2unchecked: map[-1, 0]4 bytes before the buffera read outside the map

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. Up. The first offset is up, (-1, 0). in_bounds tests r >= 0 first, and -1 fails it, so the neighbour is skipped without any read.
  2. Down. Down is (1, 0), inside all four bounds. map[1, 0] is #, the first character of ##.#, so land becomes 1.
  3. Left. Left is (0, -1): r passes, but c >= 0 fails, so this neighbour is skipped as well.
  4. Right. Right is (0, 1), the # of .#.., so land becomes 2. The program printed 2 as the first count of row 0, 2111: two of the corner's neighbours are outside the map.
  5. Without the check. land_around_unchecked reads map[-1, 0] as well, flat index -1 × 4 + 0 = -4. AddressSanitizer reported a read 4 bytes before the map's buffer; without it the program printed 2, because neither stray byte happened to be #.

Two of the four neighbours were outside the map, and the checked version never read them: each one failed a bound test first.

Key idea

Test r and c against both ends before every read: a flat index that lies inside the vector can still belong to a cell of the next row.

Checkpoint

Check: Bounds checks for neighbours

  1. Question 1 of 2 In a 3 × 4 map stored flat, the step right from (1, 3) gives the flat index 1 × 4 + 4 = 8. Which cell is that?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: (2, 0), the first cell of the next row

    Without the test c < cols, the step right from the last column reads the first cell of the next row, a real cell with the wrong position. Only the four separate bound tests catch it.

  2. Question 2 of 2 With std::size_t r = 0, what is r - 1?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The largest std::size_t, 18,446,744,073,709,551,615

    std::size_t is unsigned, so 0 - 1 wraps to the largest value, 2 to the 64th minus 1 on a 64-bit computer. With int coordinates, as std::dextents<int, 2> gives, r - 1 is -1, and r >= 0 catches it.

Practise: a Matrix with m[r, c]

Write the multidimensional operator[] of a small Matrix type over flat storage, then use it for a transpose and a product. The struct and its constructor are written for you; only the parts marked “your code here” are yours:

matrix_exercise.cpp C++ · matrix_exercise.cpp
// Exercise: a matrix in flat storage. Write the bodies marked "your code here", then build and run this file:
//   clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined matrix_exercise.cpp -o matrix_exercise
//   ./matrix_exercise
// Every check must say "ok".
#include <cstddef>
#include <print>
#include <vector>

struct Matrix {
    std::size_t rows;
    std::size_t cols;
    std::vector<int> data;   // rows * cols values, row after row

    Matrix(std::size_t r, std::size_t c) : rows(r), cols(c), data(r * c) {}

    // Element (r, c), to read or change: m[r, c] = 5;
    int& operator[](std::size_t r, std::size_t c) {
        return data[0];   // your code here
    }
    // The same for a const Matrix, which may only be read.
    const int& operator[](std::size_t r, std::size_t c) const {
        return data[0];   // your code here
    }
};

// The transpose: element (r, c) of the result is element (c, r) of m.
Matrix transpose(const Matrix& m) {
    Matrix t(m.cols, m.rows);
    // your code here
    return t;
}

// The product a × b. a.cols equals b.rows; the result has a.rows rows and b.cols columns.
Matrix multiply(const Matrix& a, const Matrix& b) {
    Matrix p(a.rows, b.cols);
    // your code here
    return p;
}

int failures = 0;

void check(bool passed, const char* what) {
    std::println("{}  {}", passed ? "ok    " : "FAILED", what);
    if (!passed) ++failures;
}

Matrix from_rows(std::size_t rows, std::size_t cols, const std::vector<int>& values) {
    Matrix m(rows, cols);
    m.data = values;
    return m;
}

int main() {
    Matrix m(2, 3);
    m[0, 2] = 7;
    m[1, 0] = 4;
    check(m[0, 2] == 7 && m[1, 0] == 4 && m[1, 1] == 0, "m[r, c] reads back what was written");
    check(m.data == std::vector<int>{0, 0, 7, 4, 0, 0}, "elements are stored row after row");

    const Matrix a = from_rows(2, 3, {1, 2, 3, 4, 5, 6});
    check(a[1, 2] == 6, "a const Matrix can be read");

    Matrix t = transpose(a);
    check(t.rows == 3 && t.cols == 2 && t.data == std::vector<int>{1, 4, 2, 5, 3, 6}, "the transpose of a 2 x 3 matrix is 3 x 2");

    Matrix p = multiply(a, t);   // (2 x 3) times (3 x 2) gives 2 x 2
    check(p.rows == 2 && p.cols == 2 && p.data == std::vector<int>{14, 32, 32, 77}, "a times its transpose");

    Matrix row = from_rows(1, 3, {1, 2, 3});
    Matrix column = from_rows(3, 1, {4, 5, 6});
    check(multiply(row, column).data == std::vector<int>{32}, "(1 x 3) times (3 x 1) is the single number 32");
    check(multiply(column, row).data == std::vector<int>{4, 8, 12, 5, 10, 15, 6, 12, 18}, "(3 x 1) times (1 x 3) is 3 x 3");

    Matrix ones(100, 100), twos(100, 100);
    for (int& x : ones.data) x = 1;
    for (int& x : twos.data) x = 2;
    Matrix big = multiply(ones, twos);
    check(big[0, 0] == 200 && big[99, 99] == 200 && big[42, 7] == 200, "100 x 100: every element is 200");

    std::println("{} of 8 checks failed", failures);
    return failures == 0 ? 0 : 1;
}

As it starts, every check fails:

The starting point Python · exercise_session.py
from session import run

# The exercise as it starts: every function body still has to be written, so every check fails.
run("""
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined matrix_exercise.cpp -o matrix_exercise
./matrix_exercise
""")

Output

$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined matrix_exercise.cpp -o matrix_exercise
matrix_exercise.cpp:17:33: warning: unused parameter 'r' [-Wunused-parameter]
   17 |     int& operator[](std::size_t r, std::size_t c) {
      |                                 ^
matrix_exercise.cpp:17:48: warning: unused parameter 'c' [-Wunused-parameter]
   17 |     int& operator[](std::size_t r, std::size_t c) {
      |                                                ^
matrix_exercise.cpp:21:39: warning: unused parameter 'r' [-Wunused-parameter]
   21 |     const int& operator[](std::size_t r, std::size_t c) const {
      |                                       ^
matrix_exercise.cpp:21:54: warning: unused parameter 'c' [-Wunused-parameter]
   21 |     const int& operator[](std::size_t r, std::size_t c) const {
      |                                                      ^
4 warnings generated.
$ ./matrix_exercise
FAILED  m[r, c] reads back what was written
FAILED  elements are stored row after row
FAILED  a const Matrix can be read
FAILED  the transpose of a 2 x 3 matrix is 3 x 2
FAILED  a times its transpose
FAILED  (1 x 3) times (3 x 1) is the single number 32
FAILED  (3 x 1) times (1 x 3) is 3 x 3
FAILED  100 x 100: every element is 200
8 of 8 checks failed
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 exercise_session.py

You are done when it prints 0 of 8 checks failed with no sanitizer report. Hints: both versions of operator[] return data[r * cols + c]; the transpose copies m[r, c] into t[c, r]; and element (i, j) of a product is the sum of a[i, k] * b[k, j] for k from 0 to a.cols - 1. Keep k in the innermost loop, or try moving it outwards and time the 100 × 100 product both ways: the loop order matters for the same reason as above.

Matrix Calculator Multiply or transpose a small matrix without working it out by hand, and compare the result with your program's output.

After the eight checks pass, try the questions below on storage, mdspan, the comma, loop order and neighbours. They begin with the topic you have missed most, and the feedback on each pick says why it is right or wrong:

Practice that adapts to you

Practise this lesson

15 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 15 A grid with 7 columns is stored row by row in a flat vector. At which index is row 2, column 3?

    Type a number.

    Show the answer: Question 1 of 15

    Answer: 17

    Rows 0 and 1 come first, 7 elements each, so row 2 starts at index 14, and column 3 is 3 further on: 2 × 7 + 3 = 17. The formula is r × cols + c.

  2. Question 2 of 15 Why does the finished grid std::vector<std::vector<int>> nested(3, std::vector<int>(4)) hold 4 allocations?

    Choose one answer.

    Show the answer: Question 2 of 15

    Answer: One for the outer vector and one for each of the 3 rows

    A vector of vectors is one vector per row inside another vector: rows + 1 allocations. Flat storage makes one for the whole grid.

  3. Question 3 of 15 An image 1,920 pixels wide and 1,080 high is stored row by row in a flat vector. At which index does its last row, row 1,079, start?

    Type a number.

    A hint

    Every row before it holds 1,920 pixels.

    Show the answer: Question 3 of 15

    Answer: 2071680

    r × cols + 0 = 1,079 × 1,920 = 2,071,680. The row ends at index 2,073,599, the last of the image's 2,073,600 pixels.

  4. Question 4 of 15 std::mdspan grid(flat.data(), 3, 4); What happens to flat when you write grid[1, 2] = 99;?

    Choose one answer.

    Show the answer: Question 4 of 15

    Answer: flat[6] becomes 99, because the mdspan is only a view of flat's elements

    An mdspan owns nothing: it holds a pointer and the extents and computes 1 × 4 + 2 = 6. It must not outlive the vector, and a reallocation of the vector would leave it pointing at freed memory.

  5. Question 5 of 15 What does an mdspan with std::dextents<std::size_t, 2> keep beside its pointer?

    Choose one answer.

    Show the answer: Question 5 of 15

    Answer: Its two sizes, chosen while the program runs

    std::dextents<std::size_t, 2> means two extents of type std::size_t, both chosen while running, so the view stores them: 8 bytes of pointer and 16 of sizes.

  6. Question 6 of 15 A function makes std::mdspan grid(flat.data(), 3, 4); and then calls flat.push_back(0);. What about grid?

    Choose one answer.

    Show the answer: Question 6 of 15

    Answer: It may dangle, because a reallocation leaves grid pointing at flat's old buffer

    An mdspan keeps flat.data() as it was when the view was made. Any call that can make flat reallocate can leave the view dangling, so make the view after the last such call.

  7. Question 7 of 15 In C++23, what does grid[1, 2] mean when grid is a std::vector<std::vector<int>>?

    Choose one answer.

    Show the answer: Question 7 of 15

    Answer: Nothing valid; vector has no operator[] with two arguments, and Clang refuses the line

    C++23 reads the comma in a subscript as two arguments to operator[], which std::vector does not have. Clang reports an error. GCC 16 warns and falls back to the old comma operator, which read grid[2], a row that does not exist, and its debug assertion stopped the program.

  8. Question 8 of 15 Before C++20, what did a[1, 2] mean for an array a?

    Choose one answer.

    Show the answer: Question 8 of 15

    Answer: a[2], because the comma operator throws away the 1

    1, 2 inside the brackets was a comma expression whose value is 2, so a[1, 2] was a[2]. Paper P1161R3 deprecated it in C++20, and C++23 gave the comma its new meaning.

  9. Question 9 of 15 In C++23, for which of these does x[1, 2] call an operator[] with two arguments?

    Choose one answer.

    Show the answer: Question 9 of 15

    Answer: A std::mdspan with two extents

    The C++23 meaning exists only for classes that declare an operator[] with more than one parameter. For every other type, write one subscript per level, or use flat storage with an mdspan.

  10. Question 10 of 15 Which loop order reads a flat, row-major grid fastest?

    Choose one answer.

    Show the answer: Question 10 of 15

    Answer: Rows in the outer loop, columns in the inner loop

    With the column index in the inner loop, each step reads the next int in memory, so all 32 ints of every 128-byte cache line that is fetched get used. Going down a column jumps a whole row ahead each time and uses one int per line. In the lesson's recording the column order was more than ten times slower for a 4096 × 4096 grid with both compilers.

  11. Question 11 of 15 Why was the column order so slow, when it reads exactly the same ints as the row order?

    Choose one answer.

    Show the answer: Question 11 of 15

    Answer: Each read lands in a different cache line and uses 4 of its 128 bytes

    Row by row, one 128-byte line serves 32 reads. Column by column, each read needs a line of its own and uses 4 of its 128 bytes; even the 1 MiB grid, which fits in the level-2 cache, was several times slower that way.

  12. Question 12 of 15 Data arrives column-major and is read through a layout_left mdspan. Which loop order reads it in memory order?

    Choose one answer.

    Show the answer: Question 12 of 15

    Answer: Columns in the outer loop, rows in the inner loop

    The fast order follows the layout: the index that changes fastest in memory goes in the inner loop. That is c for layout_right and r for layout_left.

  13. Question 13 of 15 With int coordinates, which conditions must hold before reading the neighbour (nr, nc) of a cell?

    Choose every answer that is right.

    Show the answer: Question 13 of 15

    Answer:

    • nr < rows
    • nr >= 0
    • nc < cols
    • nc >= 0

    All four bounds are needed. The last choice is not enough: for the cell to the right of the last column, the flat index is still inside the vector, but it belongs to the first cell of the next row.

  14. Question 14 of 15 How many bound tests does in_bounds make before a neighbour is read?

    Choose one answer.

    Show the answer: Question 14 of 15

    Answer: Four, r >= 0, r < rows, c >= 0 and c < cols

    in_bounds tests both ends of both coordinates, and land_around reads m[nr, nc] only when all four hold. The unchecked version read 4 bytes before the map.

  15. Question 15 of 15 Why does neighbours.cpp use std::dextents<int, 2> instead of std::size_t coordinates?

    Choose one answer.

    Show the answer: Question 15 of 15

    Answer: So that the step up from row 0 gives -1, which the test r >= 0 catches

    With std::size_t, 0 - 1 wraps to 18,446,744,073,709,551,615, so r >= 0 is always true and only r < rows stops the read. Signed coordinates turn a step outside into a negative number that the four tests catch.

Interview questions

Fresher · Why is a flat vector indexed with r * cols + c often better than a vector of vectors for a grid?

A flat vector keeps the whole grid in one block of memory: one allocation, no per-row bookkeeping, and every row directly after the one before it. A vector of vectors makes one allocation per row and scatters the rows over the heap. Creating it is slower, it uses more memory (24 bytes of row object per row on a 64-bit computer, plus each allocation’s overhead), and any access that moves between rows jumps to unrelated memory. Reading row by row is fast in both, because each row is contiguous, but column order and neighbour lookups suffer. A flat vector also hands the whole grid to C libraries and to std::mdspan as one pointer. A vector of vectors is still the right choice when rows have different lengths.

Expect two follow-ups to this answer: when nested vectors still win, and how to hand a flat grid to a function. Work through both here; the feedback names what each option gets right or leaves out:

Checkpoint

Interview round: two follow-up questions

  1. Question 1 of 2 Follow-up: when is a vector of vectors the right choice after all?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: When the rows have different lengths, such as the rows of a triangle

    Rows of different lengths are the case for nested vectors. For a rectangular grid, flat storage with an mdspan gives one allocation, contiguous rows and m[r, c].

  2. Question 2 of 2 Follow-up: how do you pass a flat grid to a function without copying it or losing its shape?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: As a std::mdspan by value, a pointer and the extents, 8 to 24 bytes

    An mdspan is cheap to copy, owns nothing and knows its extents and its layout, which makes it the parameter for a grid stored elsewhere. Like any view, it must not outlive that storage.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Mid to senior

    A shortest-path search over a 2,000 × 2,000 map stored as std::vector<std::vector<char>> is too slow and once read outside the map. How would you change the data layout and the neighbour code, and what speed-up would you honestly expect?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    A numerical library hands your C++ code a pointer to a rows × cols matrix of doubles stored column by column, Fortran style. Today you copy it into a vector of vectors to write m[r][c], and profiling shows the copy dominates. How do you remove the copy, and what must you get right?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • A vector of vectors allocates every row separately; a flat vector keeps the grid in one block, with row r, column c at index r * cols + c.
  • std::mdspan (C++23) is a view over flat storage: m[r, c] does the arithmetic, it copies and owns nothing, and its row loop ran as fast as hand-written indexing.
  • layout_right is row-major, the default; layout_left is column-major, for data from Fortran or MATLAB.
  • In C++23 grid[1, 2] is a two-argument operator[]: an error for a vector of vectors in Clang, and a warning plus a read of grid[2] in GCC 16.
  • Visit row-major grids row by row: on a 64 MiB grid, column order was more than 50 times slower with Clang and more than 10 times with GCC.
  • Before reading a neighbour, test all four bounds; use signed coordinates for the arithmetic.

Terms in this lesson

cache line
The block in which memory travels into the processor's caches, 128 bytes on this course's Mac. Reading one int brings in its whole line, so the ints next to it come almost free, while a read somewhere else waits for another line.
column-major order
Storing a grid column after column, so that the first index changes fastest: row r, column c of a grid with R rows is at r + c × R. Fortran and MATLAB store matrices this way, and std::mdspan reads it with layout_left.
extent
The size of one dimension of a std::mdspan, such as its number of rows. extent(0) and extent(1) give the two of a grid; std::extents fixes them while compiling, and std::dextents keeps them as values chosen while the program runs.
row-major order
Storing a grid row after row, so that the last index changes fastest: row r, column c of a grid with C columns is at r × C + c. C arrays of arrays use it, and so does std::mdspan's default layout, layout_right.

Corrections

  • The key takeaways now say that the mdspan row loop ran as fast as hand-written indexing rather than exactly as fast: the recorded times differ by a few per cent.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.