DEV Community

Samiur
Samiur

Posted on • Updated on

Max size of array in C++

I also got that on another site
https://kodyoon.com/

Max size of array in c++
In this post, we will learn about the Max size of array in C++. C++ has various inbuilt functions
The C++ function std::array::max_size() is used to get the maximum number of elements that can be held by array container.

Declaration
Following is the declaration for std::array::max_size() function form std::array header.

constexpr size_type max_size() noexcept;
Return Value
Returns the maximum number of elements that can be held by array container. This value is always same as the second parameter of the array template used to instantiate array.

Time complexity
Constant i.e. O(1)
Example
The following example shows the usage of std::array::max_size() function.

include

include

using namespace std;

int main(void) {

arrayarr; /* array of 10 integers */

cout << "maximum size of arr = " << arr.max_size()
<< endl;

cout << "size of arr = " << arr.size()
<< endl;

return 0;
}
this will produce the following result −

maximum size of arr = 10
size of arr = 10
The array we created contains 10 integers. And therefore, we get the maximum size = 10

C++ does not impose any limits for the dimensions of an array. But as the array has to be stored somewhere in memory, so memory-related limits imposed by other parts of the computer system apply. Note that these limits do not directly relate to the dimensions (=number of elements) of the array, but rather to its size (=amount of memory taken).

Hope this post be helpful and useful to you.

Thank you

Top comments (2)

Collapse
 
sandordargo profile image
Sandor Dargo

I'd advise you to have a look at the editor guide to see how you can format text on Dev.to and in particular the syntax highlighting part of this Markdown-cheatsheet. Following the above guides highly improves the readability of posts.

Collapse
 
samiur29980181 profile image
Samiur

Thanks for advise !