6 Ways to Find Size of Array in C++

C-Plus-Plus-array

In this article, we are going to learn 6 Ways to Find Size of Array in C++. The array is a collection of elements with a finite size. The size can not grow or shrink at runtime. To find out the size of an array in C++ we can use multiple techniques.

6 ways to Find Array Size in C++


  • Using sizeof Operator to find Size of Array in C++.
  • Using pointer to calculate size of array.
  • Using end() and begin() functions.
  • Using std::array container size() function.
  • Using std::size() Method to find Array Size in C++ 17.
  • Using std::ssize() Method to find Array Size in C++ 20.

1. Using sizeof Operator to find Size of Array in C++


In this example, we are going to use the sizeof() function. Using this function we will get the size of the full array in bytes and the size of anyone element in bytes. Both sizes will help us find the size length of the array. We will find the full length and then divide it by the size of each element. This will give us the size of the array.

#include <iostream>
using namespace std;

int main() 
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	
	int arrbytesize = sizeof(arr);
	int arrelemntsize = sizeof(arr[0]);
	
	int arrSizelength = sizeof(arr)/sizeof(arr[0]);

    cout << "The size of array is: " << arrSizelength << endl;
	
    return 0;
}

output

The size of array is: 10

Note: This is not a reliable method to find the size of the array and also it will give different results based
on the system architecture( 32 bit or 64 bit). This approach doesn’t work on pointers. As you can see below code will not give you the desired results.

int *ptr = new int[10];
std::cout << "The size of array is = " << (sizeof(ptr)/sizeof(*ptr)) << std::endl;
or:

void func(int *ptr)
{
    std::cout << "The size of array is = " << (sizeof(ptr)/sizeof(*ptr)) << std::endl;
}

2. Using pointer to calculate size of array


We can make use of the traditional method that is by using the pointers. Arrays are stored in contiguous address locations in memory. So it becomes easy to calculate the size from the addresses of the start and end element of the array.

We can get the starting pointer of the array easily as the array pointer point to start address. Also, we can calculate the address of the last element in the array.so if we subtract the first address from the last address we will get the size.

C++ Program to find Array of Size using pointer

#include <iostream>
using namespace std;

int main() {
  int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  
  // This is the end of the array
  int end = *(&arr + 1);
  
  // arr holds the start of the arr
  int arrSizelength = end - arr;
  
  cout << "The size of array is: " << arrSizelength;
  return 0;
}

output

The length of the array is: 10

3.Using end() and begin() functions


The above example makes use of the pointers to calculate the size. We could do this by finding the start and end addresses. In this example, we are going to make use of built-in functions that give us the end and beginning of the array. Once we subtract the beginning from the end, we get the size of the array. let us see this with the help of the below code.

C++ Program to find size of array using end() and begin() function

#include <iostream>
using namespace std;

int main ()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    auto arrlen = end(arr) - begin(arr);
    cout << "The length of the array is: " << arrlen << endl;
}

Output

The length of the array is: 10

4.Using std::array container size() function


We have the size() function we can call on a std::array() object to get the size of that array. This will give us the size in one shot.

C++ Program to find size of array using std::array

#include <iostream>
#include <array>

using namespace std;

int main() {
    array<int, 10> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    cout << "The length of the array is: " << arr.size() << endl;

    return 0;
}

Output

The length of the array is: 10

5. Using std::size() Method to find Array Size in C++


We have the size() function available in C++ 17. This function helps us get the size of the array and other containers.
Please not that this function will work for you only if you are using an environment that supports C++ 17 onwards (C++20,C++22).

C++ Program to find size of array std::size() method

#include <iterator>
#include <iostream>
#include <array>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	
	auto dataSize = std::size(arr);
    cout << "The length of the array is: " << dataSize << endl;

    return 0;
}

Output

The length of the array is: 10

6. By Using std::ssize() Method to find Array Size in C++


We have the ssize() function available in C++ 20. This function helps us get the size of the array and other containers.
Please not that this function will work for you only if you are using an environment that supports C++ 20 onwards(C++22).This function returns a signed value.

#include <iterator>
#include <iostream>
#include <array>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	
	auto dataSize = std::ssize(arr);
	cout << "The length of the array is: " << dataSize << endl;
    
    return 0;
}

Output

The length of the array is: 10