### get size of any type array

Does anyone know how to find the length of an arbitrary type of array? All I have found online were how to find the length of an int array.
In my case I am trying to find the length of a string array.
 ``1234567891011`` ``````#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0]))) int main() { short arr_int[]={4,1,2,4,5}; char arr[]={"abc"}; cout <

Note that this method doesn't work if array is allocated dynamically:
 ``12`` `````` char *dyn_arr=new char[100]; cout <
Thank you for your help, but can you explain to me why do you divide by sizeof((array[0]))?
`sizeof(array)` returns array size in bytes. `sizeof(array[0])` returns size of single element in array. To get number of elements in array you must divide arrays's size by size of single element.

` int arr_int[]={4,1,2,4,5};`

` sizeof(arr_int[0])` == `sizeof(int)` == 4 bytes
`sizeof(arr_int`) == 20 bytes
20/4==5 elements in array
Last edited on
Null's solution is error-prone. I don't recommend it. macros in general are evil for this reason (and others)

The templated solution is superior.

Here's a compare/contrast:

 ``123456789101112131415161718192021222324`` ``````// bad #define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0]))) // good template inline unsigned arraysize(const T (&v)[S]) { return S; } int main() { // different kinds of arrays, all size 5 int normalarray[] = {1,2,3,4,5}; std::vector vec(5); int* dynamic = new int[5]; // ARRAY_SIZE will only work for normalarray: cout << ARRAY_SIZE(normalarray); // prints 5 as expected cout << ARRAY_SIZE(vec); // prints unreliable garbage cout << ARRAY_SIZE(dynamic); // prints unreliable garbage // on the other hand, arraysize will give you an error if you use it incorrectly: cout << arraysize(normalarray); // prints 5 as expected cout << arraysize(vec); // gives you a compiler error cout << arraysize(dynamic); // gives you a compiler error }``````
Last edited on
@Disch: Could you explain how it works? Why `unsigned S` is a template parameter? And how its value equals to array size?
The code is passing an array by reference. Here's a way to make it easier to understand:

 ``1234567891011121314151617181920`` ``````typedef int int5[5]; // int5 foo; is now the same as int foo[5]; void func(const int5& v) // the typedef makes this easier to understand { // a function that takes an array of 5 ints by reference } int main() { int four[4]; int five[5]; int six[6]; func(four); // error int[4] is not int[5] func(five); // OK func(six ); // error int[6] is not int[5] int* dynamic = new int[5]; func(dynamic); // error, int* is not the same as int[5] }``````

The int5 typedef makes the function easier to understand, but you can do it without the typedef:

 ``1234`` ``````void func(const int (&v)[5]) { // same thing as above }``````

If you template it, the function can accept arrays of any size:

 ``1234567891011121314151617181920`` ``````template void func(int (&v)[S]) { cout << S; } int main() { int four[4]; int five[5]; // verbose, specifying the size in the

All I do from there is just template the type as well, so it can work with arrays of any type instead of just arrays of ints.
Now I understand, thanks.
@Disch: What does (&v) means? This is the first time I've seen it. By the way, thank you for the explanation.
It means that v is a reference.
Topic archived. No new replies allowed.