4

I suppose I'm a bit confused as to how exactly optional values are stored. When constructing a class or struct that contains std::optional<T> members, will these members be stored contiguously in memory or does optional allocate dynamically? For example, would the below struct be one contiguous block of memory?

struct Material
    {
        std::string name;
        std::optional<size_t> albedo;
        std::optional<size_t> normal;
        std::optional<size_t> metalness;
        std::optional<size_t> roughness;
        std::optional<size_t> ao; // ambient occlusion
        bool hasAlphaChannel = false;
    };
whitwhoa
  • 2,389
  • 4
  • 30
  • 61
  • 1
    Aside from `std::optional` internal storage you still have the concern of [padding](https://stackoverflow.com/questions/5397447/struct-padding-in-c) that may result in your members being non-contiguous. – Cory Kramer Jun 10 '21 at 14:21

2 Answers2

11

optional is required to not use dynamic allocation.

If an optional contains a value, the value is guaranteed to be allocated as part of the optional object footprint, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though operator*() and operator->() are defined.

https://en.cppreference.com/w/cpp/utility/optional

There is also the member variables of optional and also padding that can occur. so no, they are not necessarily contigous, but they are within the object you declare them in.

Raildex
  • 3,406
  • 1
  • 18
  • 42
3

According to the standard std::optional is prohibited to use dynamic memory for their direct members.

One possible layout could for example be:

template<class T>
class optional
{
    bool engaged;
    union {
        T value;
        unsigned char reserved;
    };
};
Mestkon
  • 3,532
  • 7
  • 18