How to Find the Size of an Array in C++
C++ arrays don’t carry a handy .length property like arrays in some other languages, so finding how many elements one holds trips up almost every beginner. The good news: there are two reliable ways to do it, plus one big trap you need to know about.
The Classic sizeof Trick
sizeof tells you how many bytes something occupies. An array’s total bytes divided by the bytes of a single element gives the number of elements:
#include <iostream>
int main() {
int scores[] = {90, 85, 70, 100, 60};
int length = sizeof(scores) / sizeof(scores[0]);
std::cout << "The array has " << length << " elements\n"; // 5
return 0;
}
sizeof(scores) is the whole array (5 ints), and sizeof(scores[0]) is one int. Divide them and you get 5. This works for any element type because the division cancels out the per-element size.
The Modern Way: std::size (C++17)
Since C++17 there’s a cleaner option that reads like plain English — std::size, from the <iterator> header:
#include <iostream>
#include <iterator> // for std::size
int main() {
double prices[] = {1.99, 2.49, 0.99};
std::cout << "Elements: " << std::size(prices) << "\n"; // 3
return 0;
}
std::size returns the element count directly, so there’s no dividing and no chance of mixing up the two sizeof calls. Prefer it whenever your compiler supports C++17 or newer.
The Big Gotcha: Arrays Decay to Pointers
Here’s the trap. Both tricks above only work where the array was declared. The moment you pass an array into a function, it “decays” into a pointer to its first element — and the size information is gone:
#include <iostream>
void printSize(int arr[]) {
// arr is really a pointer here, not the whole array
std::cout << "Inside function: " << sizeof(arr) / sizeof(arr[0]) << "\n";
}
int main() {
int data[] = {1, 2, 3, 4, 5, 6};
std::cout << "In main: " << sizeof(data) / sizeof(data[0]) << "\n"; // 6
printSize(data); // prints 2 on a typical 64-bit system, NOT 6
return 0;
}
In main, sizeof sees the real array and prints 6. Inside printSize, sizeof(arr) is the size of a pointer (8 bytes) divided by the size of an int (4 bytes), giving a meaningless 2. The fix is simple: pass the length as a second argument. This is exactly why std::size is safer — it refuses to compile on a decayed pointer instead of giving a wrong answer.
std::array Knows Its Own Size
If you want a fixed-size array that does remember its length everywhere, use std::array. It behaves like a built-in array but carries a .size() method that always works:
#include <iostream>
#include <array>
int main() {
std::array<int, 4> nums = {10, 20, 30, 40};
std::cout << "Size: " << nums.size() << "\n"; // 4
return 0;
}
For a resizable list, std::vector works the same way with .size(). Both keep their length when passed to functions, which sidesteps the decay problem entirely.
Quick Reference
| Situation | How to get the size |
|---|---|
| Stack array, same scope | sizeof(a) / sizeof(a[0]) |
| C++17 or later | std::size(a) |
| Inside a function | pass the length as a parameter |
| Need a resizable list | use std::vector and .size() |
| Fixed size, but safer | use std::array and .size() |
Related Articles
- C++ Arrays Tutorial — declaring and using arrays
- How to Pass an Array to a Function in C++ — handle the decay problem
- C++ Array vs Vector — when to switch to a vector
- C++ Vector Tutorial — resizable lists that track their size
- C++ 2D Arrays — sizing arrays with more than one dimension
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.