Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example where std::array::max_size and std::array::size gives different result

Whenever I try with max_size() and size() funtion of std::array, I get same results, I wanted to know if there could be a situation where two of them give different results.

like image 761
user1718009 Avatar asked Jan 19 '18 18:01

user1718009


People also ask

Is std :: array fixed size?

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 array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically.

Is std :: array slower than C array?

It will provide a value like semantics equally to the other C++ containers. A std::array should have same runtime performance as a c-style array.

What is the difference between std :: array and array?

std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.

Is std :: array contiguous?

Yes the memory of std::array is contiguous.


2 Answers

Those two functions will always return the same value, namely N for an std::array<T, N>.

They are provided for consistency with other containers, for which the values will differ (e.g. std::vector).

like image 86
Baum mit Augen Avatar answered Sep 24 '22 19:09

Baum mit Augen


That function exists for compatibility with other containers like std::vector. For std::array those two values will always be the same.

like image 38
SergeyA Avatar answered Sep 26 '22 19:09

SergeyA