How to know the size of a const array?

19,302

Solution 1

You can use int size = sizeof(codesArr) / sizeof(myStr); as long as you don't pass it as parameter to a function, because then the array will decay into a pointer and lose size information.

You can also use a template trick:

template<typename T, int sz>
int size(T(&)[sz])
{
    return sz;
}

Solution 2

The safe and clear way to do this is to use std::extent (since C++11):

const size_t size = std::extent<decltype(codesArr)>::value;

Solution 3

The best way(pre c++11) to get size of an array is given at this link. It is a convoluted template trick, but has many advantages over other approaches as is discussed in the above link. The method is to use the following macro+function:

template <typename T, size_t N>
char ( &_ArraySizeHelper( T (&array)[N] ))[N];

#define countof( array ) (sizeof( _ArraySizeHelper( array ) ))

Its advantages, among others, are that it is purely a compile time operation, which is better than doing it in run time, and , before c++11's constexpr, is the only method that allows you to use the result of the operation as the size of another array ( since its a compile time constant

Share:
19,302

Related videos on Youtube

Dardar
Author by

Dardar

Updated on September 17, 2022

Comments

  • Dardar
    Dardar over 1 year

    given the following code:

    const myStr codesArr[] =  {"AA","BB", "CC"};     
    

    myStr is a class the wraps char*. I need to loop over all the items in the array but i don't know the number of items. I don't want to define a const value that will represent the size (in this case 3)

    Is it safe to use something like:

    const int size = sizeof(codesArr) / sizeof(myStr);
    

    what to do?

    • Grizzly
      Grizzly over 11 years
      Is this question c or c++? For c++ I would really recommend using std::array (or std::tr1::array if your compiler isn't c++11) instead of builtin arrays. And likely using std::string instead of whatever myStr does
    • Some programmer dude
      Some programmer dude
      Optionally you could also do sizeof(codesArr) / sizeof(codesArr[0]);, with the restriction mentioned by Luchian Grigore.
  • nos
    nos over 11 years
    @Maroun85 Yes, there's an implicit conversion (not a cast per se). This conversion will not cause any problem as long as the size can be represented as an int.
  • Nawaz
    Nawaz over 11 years
    Your size() function should be constexpr in C++11, or use macro technique in C++03, so that it can be used as constant expression.
  • ecatmur
    ecatmur over 11 years
    @Nawaz not possible; constexpr functions can only have literal-type parameters, even if they aren't used.
  • Nawaz
    Nawaz over 11 years
    @ecatmur: Oh I didn't know that. Anyway, in that case, macro technique is the way.
  • James Kanze
    James Kanze over 11 years
    What's so convoluted about it? It is a straightforward use of template argument deduction. (If you want convoluted template tricks, look at some of the Boost sources.)
  • James Kanze
    James Kanze over 11 years
    (Of course, there is the fact that your exact code has undefined behavior, since it contains a symbol which starts with an underscore followed by a capital letter.)
  • Jens Gustedt
    Jens Gustedt over 11 years
    is there any particular reason not to use size_t?
  • Luchian Grigore
    Luchian Grigore over 11 years
    @JensGustedt no, I guess size_t is more appropriate.