Easiest way to get std :: array memory size?

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

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

Edit: my question was not accurate. By “size in memory” I mean the size of all the elements (ourselves) contained in the array, so for example, they are pointers pointing to structures, I want only the size of the pointers, not the structures that they point to. I also do not want to include the size of the possible overhead in the implementation of std::arr . Just elements of an array.

Some people suggested sizeof(arr) . This: What is sizeof std :: array <char, N>? asks not to agree. And although it seems that it works on my machine , I want to know what the standard guarantees.

+5
source share
5 answers

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 in wandbox


From cppreference :

std::array is a container that encapsulates arrays of fixed size.

This container is an aggregate type with the same semantics as a struct containing a C-style T[N] array as the only non-static data element.

+5
source

There is 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 may be called (but unspecified) members and / or an unnamed appendix.

The guarantee stems from the fact that the wrapped array T[N] must be the first member of std::array<T,N> , but no other members are specified.

+2
source

Read the std::array documentation . So yes, I guess. Or try maybe

  (arr.size()-1) * sizeof(arr.value_type) + sizeof(std::array<T,1>) 

But I would just use sizeof(arr)

By the way, I'm not sure that you have an official guarantee about this. I assume that the standard theoretically allows std::array be the same as std::vector , except that resize() and some other methods will be hidden. But no reasonable implementation will do this (since std::array was invented to pack simple arrays in a container like vectors).

Perhaps an implementation that allows only std::array -s to have no more than two elements (but except an exception, an exception) may correspond to the letter of the standard.

0
source

I realized that you were asking: what is the size of the memory occupied by the ensemble of the element array<value_type,N> arr , which is between arr.begin() and arr.end() ?

The answer is sizeof(value_type)*N , this is specified in the standard, but some processing is required to get this result.

In the C ++ standard [dcl.array] (this is about (c-) an array not std::array ):

An array type object contains an adjacent nonempty set of N subobjects of type T.

in [expr.add] (here also the term array refers to a (c-) array):

When an expression that has an integral type is added or subtracted from the pointer, the result is the type of the operand of the pointer. If the expression P points to the element x [i] of the object of the array x with n elements, 86 expressions P + J and J + P (where J has the value j) indicate the (possibly hypothetical) element x [i + j], if 0 ≤ i + j ≤ n; otherwise, the behavior is undefined. Similarly, the expression P - J indicates a (possibly hypothetical) element x [i - j] if 0 ≤ i - j ≤ n; otherwise, the behavior is undefined.

And in [array.data] (here the term array refers to std::array ):

 constexpr T* data() noexcept; constexpr const T* data() const noexcept; 

Returns: a pointer such that data () == addressof (front ()) and [data (), data () + size ()) is a valid range.

So, data() returns a valid range for the std::array element, this range is iterated using a pointer to value_type, so it follows the arithmetic of the pointer, which follows the rule for (c-) indexing the array and the elements a (c-) array are adjacent . QED

0
source

Since no one posted anything better than my first guess, and sizeof(arr) most likely does NOT guarantee to not include the size of any possible additional std::array's fields. I choose this as the accepted answer.

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

If someone comes up with something better, I would be happy to accept their answer.

0
source

Source: https://habr.com/ru/post/1272162/


All Articles