In C++, generally, we use the sizeof() operator to find the size of arrays. But there are also some other ways using which we can find the size of an array. In this article, we will discuss some methods to determine the array size in C++ without using sizeof() operator.
Methods to Find the Size of an Array without Using sizeof() Operator
Given an array (you don’t know the type of elements in the array), find the total number of elements in the array without using the sizeof() operator. So, we can use the methods mentioned below:
- Using pointer hack
- Using Macro Function
- Using own self-made sizeof( )
- Using Template Function
- Using a Sentinel Value
- Using a Class or Struct
1. Using Pointer Hack
The following solution is concise when compared to the other solution. The number of elements in an array A can be found using the expression:
// &arr returns a pointer
int size = *(&arr + 1) - arr;
How Does this Method Work?
Here the pointer arithmetic does its part. We don’t need to explicitly convert each of the locations to character pointers.
- &arr – Pointer to an array of 6 elements. [See this for difference between &arr and arr]
- (&arr + 1) – After adding 1 to the address of array of 6 integers, the pointer will point to the memory location immediately after the end of the array.
- *(&arr + 1) – Same address as (&arr + 1), but type of pointer is “int *”.
- *(&arr + 1) – arr – Since *(&arr + 1) points to the address 6 integers ahead of arr, the difference between two is 6.
C++ Program to Find the Size of an Array Using Pointer Hack
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int size = *(&arr + 1) - arr;
cout << "Number of elements in arr[] is " << size;
return 0;
}
|
OutputNumber of elements in arr[] is 6
Complexity Analysis
- Time complexity: O(1)
- Auxiliary space: O(1)
2. Using Macro Function
We can define a macro that calculates the size of an array based on its type and the number of elements.
#define array_size(arr) (sizeof(arr) / sizeof(*(arr)))
- array_size(arr): Name of the macro
- (sizeof(arr): size of entire array in bytes
- sizeof(*(arr)): size of single element in bytes
Dividing the total size of the array by the size of a single element gives the number of elements in the array.
C++ Program to Find the Size of an Array using Macro
C++
#include <bits/stdc++.h>
using namespace std;
#define array_size(arr) (sizeof(arr) / sizeof(*(arr)))
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int size = array_size(arr);
cout << "Number of elements in arr[] is " << size;
return 0;
}
|
OutputNumber of elements in arr[] is 6
Complexity Analysis
- Time complexity: O(1)
- Auxiliary space: O(1)
3. Implement Our Own sizeof( )
Using custom user-defined sizeof function which can provide the functionality same as sizeof( ).
C++ Program to Implement Custom User-Defined sizeof Function
C++
#include <iostream>
using namespace std;
#define my_sizeof(type) \
(( char *)(&type + 1) - ( char *)(&type))
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int size = my_sizeof(arr) / my_sizeof(arr[0]);
cout << "Number of elements in arr[] is " << size;
return 0;
}
|
OutputNumber of elements in arr[] is 6
Complexity Analysis
- Time complexity: O(1)
- Auxiliary space: O(1)
To know more about the method refer to Implement our own sizeof.
4. Using Template Function
We can use a template function to find the size of an array.
C++ program to Find the Size of an Array Using Template Function
C++
#include <bits/stdc++.h>
using namespace std;
template < typename T, size_t N>
int array_size(T (&arr)[N])
{
return N;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int size = array_size(arr);
cout << "Number of elements in arr[] is " << size;
return 0;
}
|
OutputNumber of elements in arr[] is 6
Explanation
The array_size() function is a template function that takes two parameters:
- T: Type of array elements
- N: Size of the array
The parameter T (&arr)[N] means the function accepts a reference to any type and any size of array. When array_size function is called with the array name as parameter, T is deduced as int and N is deduced as 6.
Complexity Analysis
- Time complexity: O(1)
- Auxiliary space: O(1)
5. Using a Sentinel Value
We can add a special value to the end of the array to indicate the end of the array, and then loop through the array until you find the sentinel value. This method is commonly used in string manipulation.
C++ program to Find the Size of an Array using a Sentinel Value
C++
#include <iostream>
using namespace std;
int main()
{
const int MAX_SIZE = 100;
int arr[MAX_SIZE];
int sentinel = -1;
int i = 0;
while (i < MAX_SIZE) {
cout << "Enter a value for element " << i
<< " (or -1 to stop): " ;
cin >> arr[i];
if (arr[i] == sentinel) {
break ;
}
i++;
}
int size = i;
cout << "Size of the array: " << size << endl;
return 0;
}
|
OutputEnter a value for element 0 (or -1 to stop): Enter a value for element 1 (or -1 to stop): Enter a value for element 2 (or -1 to stop): Enter a value for element 3 (or -1 to stop): Enter a value for el...
Output
Enter a value for element 0 (or -1 to stop): 3
Enter a value for element 1 (or -1 to stop): 7
Enter a value for element 2 (or -1 to stop): 2
Enter a value for element 3 (or -1 to stop): 9
Enter a value for element 4 (or -1 to stop): 5
Enter a value for element 5 (or -1 to stop): -1
Size of the array: 5
Complexity Analysis
- Time complexity: O(n), where n is the number of elements in the array.
- Auxiliary space: O(1)
6. Using a Class or Struct
We can define a class or struct that contains an array as a member variable, and then define a member function that returns the size of the array. This method is useful if we need to pass the array and its size as a single parameter to a function.
C++ program to Find the Size of an Array using a Class and Member Function
C++
#include <iostream>
using namespace std;
class MyArray {
private :
int arr[100];
int size;
public :
void readArray();
int getSize();
};
void MyArray::readArray()
{
size = 0;
while (size < 100) {
cout << "Enter a value for element " << size
<< " (or -1 to stop): " ;
cin >> arr[size];
if (arr[size] == -1) {
break ;
}
size++;
}
}
int MyArray::getSize()
{
return size;
}
int main()
{
MyArray a;
a.readArray();
int size = a.getSize();
cout << "Size of the array: " << size << endl;
return 0;
}
|
OutputEnter a value for element 0 (or -1 to stop): Enter a value for element 1 (or -1 to stop): Enter a value for element 2 (or -1 to stop): Enter a value for element 3 (or -1 to stop): Enter a value for el...
Output
Enter a value for element 0 (or -1 to stop): 8
Enter a value for element 1 (or -1 to stop): 4
Enter a value for element 2 (or -1 to stop): 2
Enter a value for element 3 (or -1 to stop): 7
Enter a value for element 4 (or -1 to stop): 5
Enter a value for element 5 (or -1 to stop): 6
Enter a value for element 6 (or -1 to stop): -1
Size of the array: 6
Complexity Analysis
- Time complexity: O(1), getSize() method returns size member variable that takes constant time.
- Auxiliary space: O(1)