Computing length of array

I have a C++ array declared as mentioned below:

CString carray[] =
{
        "A",
        "B",
        "C",
        "D",
        "E"
}

I want to determine the length of carray at runtime. I am doing:

int iLength = sizeof(carray)/sizeof(CString);

Is this correct?

Answers


Yes. In case the declared element type ever changes, you could also write

int iLength = sizeof(carray)/sizeof(carray[0]);

You can use the following function template. If you're using Boost, you can call boost::size.

template <typename T, std::size_t N>
std::size_t size(T (&)[N])
{
    return N;
}

int iLength = size(carray);

As others have already stated, however, you should prefer std::vector to C-style arrays.


That is correct, as it is using metaprogramming as this:

template <typename T, std::size_t N>
inline std::size_t array_size( T (&)[N] ) {
   return N;
};

You must know that this works when the compiler is seeing the array definition, but not after it has been passed to a function (where it decays into a pointer):

void f( int array[] )
{
   //std::cout << array_size( array ) << std::endl; // fails, at this point array is a pointer
   std::cout << sizeof(array)/sizeof(array[0]) << std::endl; // fails: sizeof(int*)/sizeof(int)
}
int main()
{
   int array[] = { 1, 2, 3, 4, 5 };
   f( array );
   std::cout << array_size( array ) << std::endl; // 5
   std::cout << sizeof(array)/sizeof(array[0]) << std::endl; // 5 
}

This code is correct but in most circumstances there are better ways to handle arrays in C++. Especially since this method won't work with dynamically sized arrays.

For such cases, use the standard library class std::vector that represents an array of dynamic size (i.e. you can insert and remove entries).


Yes, this is the correct way to do it, but it will only work in this situation where the size of array is known at compile time and is seen at the site of the sizeof( array ) statement. It will not work with dynamically sized arrays - you will need other methods for them like using a container like stl::vector or passing/storing the number of elements as a separate parameter.


That's not runtime, it's compile time. The way you're using is correct. Note that Visual Studio defines a _countof function that does the same.

At runtime, the length cannot be determined. You either keep a length yourself, or use std::vector


If you have a dynamic array, you should probably use something like an STL vector. http://www.cppreference.com/wiki/stl/vector/start

Normal arrays in C++ are fixed size and you need to manually allocate memory to expand dynamic ones.

If you know your array size at compile time, use a constant rather than a calculation.


Read this article by Ivan J. Johnson in Dr. Dobbs Journal. I think it covers most of the solutions presented here. It also outlines the merits and demerits of each approach very nicely.


Windows SDK (i.e. windows.h header) offers the macro ARRAYSIZE which implements this functionality in a safe manner(i.e. doesn't work with non-static arrays)


The best way is to use a macro and use that where ever you want the size.

#define MAX_ROWS 1048
int array[MAX_ROWS];

This way you can use the MAX_ROWS even in a function where the array is passed as an argument.


Need Your Help

How to open a popup window in javascript using onclick?

javascript

How can I display the popup below using the onclick method in JavaScript with a button?

Django + dbxml + Apache = problems. Any solutions?

django apache mod-wsgi berkeley-db berkeley-db-xml

I'm trying to set up a Django application using WSGI. That works fine. However, I am having some issues with part of my Django app that uses BDB XML. My Apache config is as follows: