Finding Length in C++ Arrays
What Will You Learn in This Guide?
In this guide, you will learn step by step the 5 most practical methods to find the length (number of elements) of C++ arrays.
You will see both the classical sizeof() and modern STL (Standard Template Library) methods and understand which method to use in which situation.
1. Finding Length by Counting Each Element
The most basic method is to loop through the array and count each element.
Using range-based for loop (for-each) for this process makes the code simple and safe.
#include <iostream>
using namespace std;
int main() {
int c = 0;
int arr[] = {1, 2, 3, 4, 5, 6};
for (auto i : arr) { // Her elemanı sayıyoruz
c++;
}
cout << "Dizinin uzunluğu: " << c << endl;
return 0;
}
- Description:
Each time the loop runs, the counter is incremented and at the end, the total number of elements is found.
2. Using begin() ve end() Functions
The begin() and end() functions in the C++ STL return the starting and ending addresses of the array. The difference of these two addresses gives the string length.
#include <iostream>
#include <array>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40};
cout << "Dizi uzunluğu: " << end(arr) - begin(arr);
return 0;
}
- Description:
The operation end(arr) – begin(arr) returns the total number of elements in the array.
3. Finding String Length with sizeof() Operator
The sizeof() operator returns the size, in bytes, of the variable or array in memory. If you divide this value by the size of each element, you can find the number of elements.
#include <iostream>
using namespace std;
int main() {
int arr[] = {5, 10, 15, 20};
int len = sizeof(arr) / sizeof(arr[0]);
cout << "Dizinin uzunluğu: " << len;
return 0;
}
- Description:
sizeof(arr) gives the total bytes of the array, sizeof(arr[0]) gives the bytes of a single element.
Limitation of sizeof() Operator
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr;
int size = sizeof(ptr) / sizeof(ptr[0]); // Yanlış sonuç
- Problem:
sizeof(ptr) only returns the size of the pointer (e.g. 8 bytes), not the array. That's why sizeof() should only be used on static arrays.
4. STL size() Function (Modern C++ Method)
The std::array and std::vector classes that come with modern C++ (C++11 and later) have a built-in size() method.
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> arr = {1, 2, 3, 4, 5};
cout << "Dizi uzunluğu: " << arr.size();
return 0;
}
- Description:
The arr.size() method automatically returns the number of elements in the array.
5. Calculating String Length with Pointer
In C++, arrays are stored in contiguous areas in memory. By taking this address difference with pointers, the length of the array can be found.
#include <iostream>
using namespace std;
int main() {
int arr[6] = {5, 4, 3, 2, 1, 0};
int len = *(&arr + 1) - arr;
cout << "Dizinin uzunluğu: " << len;
return 0;
}
- Description:
&arr + 1 represents the next address from the end of the array. The difference between this address and arr gives the number of elements.
Difference Between Static and Dynamic Arrays
| Genre | Definition | Can the Size Be Changed? | Memory Location |
|---|---|---|---|
| Static Array | It is created at compile time. | ❌ No | Stack |
Dynamic Array (new[]) | It is created at runtime. | ✅ Yes | Heap |
// Statik
int statikArr[5];
// Dinamik
int* dinamikArr = new int[5];
The Modern Alternative: STL Containers
- In C++, std::array and std::vector are modern, safe alternatives to arrays.
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
cout << "Vector uzunluğu: " << vec.size();
return 0;
}
- Advantage:
std::vector grows or shrinks dynamically. You don't need to manually manage memory.
Performance Table
| Method | Memory Management | Performance | Security |
|---|---|---|---|
| Static Array | Compile Time | High | Medium |
Dynamic Array (new[]) | Manual | Medium | Low |
std::vector | Auto | High | High |
- Recommendation: In production environments always prefer std::vector or std::array.
Frequently Asked Questions (FAQ)
- Why doesn't sizeof() work on dynamic arrays?
Because sizeof() only returns the size of the pointer, not the array in the heap.
- Is array length the same as array size?
No. “Length” refers to the number of elements and “Size” refers to the total byte size in memory.
- How to pass array length to a function?
Since the array is passed as a pointer to the function, its length must be sent as a manual parameter.
- What is the safest method in modern C++?
Definitely using std::vector or std::array.
Result
In this guide, we have seen 5 different methods to find the length of C++ arrays. The most practical and modern solution is the size() method of STL containers. However, sizeof(arr)/sizeof(arr[0]) is still a fast and valid method when working with static arrays.
- If you want to try your codes immediately, you can create your own C++ environment on the GenixNode platform and run these examples.

