Pages

Gin Rummy Indian Rummy Nine Men's Morris and more Air Attack

Tuesday 30 August 2016

Finding size of an array in C/C++

It is not a good idea to hard code the size of an array. Instead we would like to somehow retrieve the size from it.

In some cases we will just initialize the array without specifying its size. In this we don't want to hard code the size as in future we might want to add more values to it.

An old way(the C way) of retrieving the size is to calculate the size of the whole array and then divide it with the size of an element in the array. This can be done as,
int i[] = {1, 2, 3};
int size = sizeof(i)/sizeof(i[0]);

// Usually please will write a macro to do this
#define ARRAY_SIZE(a) (sizeof((a))/sizeof((a[0])))


In C++ there is a way to retrieve the size of an array using templates.
template<typename T, std::size_t size>
std::size_t arrSize(T(&)[size]) {
    return size;
}

int i[] = { 1, 2, 3, 4 };
int size = arrSize(i);  // size will have 4 after this statement is executed

Another approach is to use std::array available from C++11. The only downside is that we should know the size while declaring the array
std::array<int, 4> a = { 1, 2, 3, 4 }; // Initialize as { { 1, 2, 3, 4 } } in C++11
int size = a.size(); // size will have 4 after this statement is executed

Another approach is to use std::vector(may be a little heavy if you are just looking for a fixed size array), using the universal initialize available from C++11.
std::vector<int> a = { 1, 2, 3, 4 };
int size = a.size(); // size will have 4 after this statement is executed

No comments:

Post a Comment