c++sizeofstdarray

Simplest way to get memory size of std::array's underlying array?


Is this the simplest/shortest way to get size in memory of the content of what std::array::data() returns?

arr.size() * sizeof(arr.value_type)

Edit: My question wasn't precise. By "size in memory" I mean size of all elements (themselves) contained in the array so if e.g. they are pointers pointing to structures, I want the size of the pointers alone, not the structures pointed to. I also don't want to include the size of any possible overhead of the std::arr implementation. Just the array elements.

Some people suggested sizeof(arr). This: What is the sizeof std::array<char, N>? begs to disagree. And while it seems to work on my machine I want to know what the standard guarantees.


Solution

  • Since no one has posted anything better than my first guess in question and sizeof(arr) is most likely NOT guaranteed to not include the size of any possible additional std::array's fields I'm choosing this as the accepted answer.

    arr.size() * sizeof(arr.value_type)
    

    Should anyone come up with anything better I'd be happy to accept their answer instead.