Simplest way to get memory size of std::array's underlying array?
You can use the sizeof
operator directly on your std::array
instance:
sizeof(arr)
Example:
struct foo
{
int a;
char b;
};
int main()
{
std::array<foo, 10> a;
static_assert(sizeof(foo) == 8);
static_assert(sizeof(a) == 80);
}
live example on wandbox
From cppreference:
std::array
is a container that encapsulates fixed size arrays.This container is an aggregate type with the same semantics as a
struct
holding a C-style arrayT[N]
as its only non-static data member.
There's no guarantee that sizeof(std::array<T,N>) == N*sizeof(T)
, but it is guaranteed that sizeof(std::array<T,N>) >= N*sizeof(T)
. The extra size might be named (but unspecified) members and/or unnamed padding.
The guarantee follows from the fact that the wrapped T[N]
array must be the first member of std::array<T,N>
, but other members aren't specified.