Search code examples
arraysc++11stdatomic

static std::atomic<foo *> bar[CONSTANT_BAZ], is it initialized or not?


I have an array of atomic pointers at global scope. Are these initialized to nullptr, or is it necessary to do this by hand, e.g.

// Found a reason to use double-braces, but is it needed??
static std::atomic<foo *> bar[CONSTANT_BAZ] {{nullptr}};

Solution

  • atomics are treated as built-in types (to be more precise, they have a trivial default constructor) and as such, a global array of them will automatically be zero initialized to nullptr.

    Only in the local case, you'd have to initialize it manually, but even then, you don't have to use double braces or nullptr. This would suffice:

    void baz() {
        std::atomic<foo *> bar[CONSTANT_BAZ]{};
    }