Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Structure of arrays (SoA) vs. array of structures (AoS) in C++

Compare storing data by record and by field, then choose a layout based on access patterns, cache locality, and the cost of converting between layouts.

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

Syntax

#include <array>
#include <cstddef>

struct Sample { int value; int tag; };

// AoS: one element per record.
std::array<Sample, 3> aos{};

// SoA: one array per field.
template<std::size_t N>
struct SamplesSoA {
    std::array<int, N> values{};
    std::array<int, N> tags{};
};
SamplesSoA<3> soa{};

// For 0 <= i < 3: aos[i].value corresponds to soa.values[i].

Data-oriented design starts with how the program uses its data. An array of structures (AoS) stores consecutive records, grouping the fields of each record together. A structure of arrays (SoA) stores each field in a separate array: values[i] and tags[i] belong to the same logical record. SoA is a layout pattern, not a dedicated C++23 language construct or standard library type.

For a sequential scan of a hot column—a frequently used field—SoA puts successive useful values next to one another. A cache line can then hold more useful values and less unused data than when reading the same field from successive AoS records. That can improve cache locality. When an operation uses most fields of a record together, AoS may be a better fit.

The conversion shown here copies fields and uses extra storage while both layouts coexist. Neither layout guarantees a speedup: workload, access pattern, compiler, and hardware all matter. Benchmark optimized builds on representative data, including any conversion the real program performs.

Keep column lengths equal and preserve the correspondence between positions. In the example, std::array shares the size parameter N across columns. For dynamic std::vector columns, encapsulate updates so the columns stay in sync even if an insertion fails. Vector reallocation invalidates pointers, references, and iterators to its elements.

Examples

#include <array>
#include <cstddef>
#include <iostream>

struct Sample {
    int value;
    int tag;
};

template<std::size_t N>
struct SamplesSoA {
    std::array<int, N> values{};
    std::array<int, N> tags{};
};

int main() {
    const std::array<Sample, 3> aos{{{3, 100}, {5, 200}, {7, 300}}};
    SamplesSoA<aos.size()> soa{};

    for (std::size_t i = 0; i < aos.size(); ++i) {
        soa.values[i] = aos[i].value;
        soa.tags[i] = aos[i].tag;
    }

    int aos_sum = 0;
    for (const Sample& sample : aos) {
        aos_sum += sample.value;
    }

    int soa_sum = 0;
    for (int value : soa.values) {
        soa_sum += value;
    }

    std::cout << "AoS: " << aos_sum << '\n'
              << "SoA: " << soa_sum << '\n';
}

Result

AoS: 15
SoA: 15

The program copies the same three records from AoS to SoA. Both passes sum only `value` and yield `15`. This compares results; it does not measure performance.

Related lessons

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

Sources