Getting Array Size in C++
Fundamental Size Calculation
In C++, built-in (C-style) arrays have a fixed size that is part of their type and determined at compile time. To find the number of elements, a common method is to use the `sizeof` operator.
The total size of the array in bytes divided by the size of one element gives the element count.
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
size_t totalBytes = sizeof(numbers); // e.g., typically 20 bytes if int is 4 bytes
size_t elementSize = sizeof(numbers[0]); // typically 4 bytes on many platforms
size_t elementCount = sizeof(numbers) / sizeof(numbers[0]);
cout << "totalBytes = " << totalBytes << '\n';
cout << "elementSize = " << elementSize << '\n';
cout << "elementCount = " << elementCount << '\n';
return 0;
}
totalBytes = 20 elementSize = 4 elementCount = 5
Template-Based Size Calculation
Templates can preserve array size information when arrays are passed to functions, making it possible to calculate size reliably at compile time.
#include <iostream>
using namespace std;
template <typename T, size_t N>
constexpr size_t array_size(T (&)[N]) {
return N;
}
int main() {
int nums[] = {10, 20, 30, 40, 50};
size_t count = array_size(nums);
cout << "count = " << count;
return 0;
}
Standard Library Alternatives (C++17+)
Modern C++ introduces standard library utilities for safer size queries, avoiding manual calculations.
#include <array>
#include <iterator> // std::size (C++17)
#include <iostream>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
size_t size1 = arr.size();
cout << "std::array size = " << size1 << '\n';
int c_array[] = {1, 2, 3};
size_t size2 = std::size(c_array); // works for C-style arrays too
cout << "C-style array size = " << size2 << '\n';
return 0;
}
Multidimensional Arrays
`sizeof` yields the total size of the whole array object. Dividing by the size of the first subarray gives the first (outer) dimension; divide again to get the next dimension.
#include <cstddef>
#include <iostream>
using namespace std;
int main() {
int m[3][4];
size_t rows = sizeof(m) / sizeof(m[0]); // 3
size_t cols = sizeof(m[0]) / sizeof(m[0][0]); // 4
cout << rows << "x" << cols << '\n';
return 0;
}
Common Pitfalls
Scenario | Problem | Solution |
---|---|---|
Array passed to function | Array decays to pointer; `sizeof` returns pointer size | Pass size explicitly, or use templates / `std::array` / `std::span` (C++20) |
Dynamic arrays | `sizeof` gives pointer size, not element count | Store size separately or use `std::vector` |
Empty arrays (C-style) | Zero-length C-style arrays are not standard C++ (some compilers allow as an extension) | Use `std::array |
Multidimensional arrays | `sizeof` alone gives total size; naïve division can be confusing | Use `sizeof` stepwise per dimension or helper templates |
Practical Example: Bounds-Safe Array Printing
Templates allow safe array printing by automatically deducing the number of elements at compile time, preventing buffer overruns.
#include <iostream>
using namespace std;
template <typename T, size_t N>
void safe_print_array(T (&arr)[N]) {
cout << "Array has " << N << " elements:" << '\n';
for (size_t i = 0; i < N; ++i) {
cout << " [" << i << "] = " << arr[i] << '\n';
}
}
int main() {
float temps[] = {98.6f, 99.2f, 100.1f, 97.9f};
safe_print_array(temps);
return 0;
}
Array has 4 elements: [0] = 98.6 [1] = 99.2 [2] = 100.1 [3] = 97.9