38

Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526

4 Answers4

23

Using C++11, the non-macro form is:

char arrname[5];
size_t count = std::extent< decltype( arrname ) >::value;

And extent can be found in the type_traits header.

Or if you want it to look a bit nicer, wrap it in this:

template < typename T, size_t N >
size_t countof( T ( & arr )[ N ] )
{
    return std::extent< T[ N ] >::value;
}

And then it becomes:

char arrname[5];
size_t count = countof( arrname );

char arrtwo[5][6];
size_t count_fst_dim = countof( arrtwo );    // 5
size_t count_snd_dim = countof( arrtwo[0] ); // 6

Edit: I just noticed the "C" flag rather than "C++". So if you're here for C, please kindly ignore this post. Thanks.

Kurt Hutchinson
  • 2,959
  • 23
  • 30
  • One difference: at least on MSVC, std::extent< decltype( "Character Literal" ) >::value returns 0, while _countof returns the appropriate count. – Ash Jul 10 '13 at 01:01
  • Also tested it on GCC and that too, returns 0. – Ash Jul 10 '13 at 01:14
  • 7
    You have to make your countof function constexpr, otherwise it won't work. Also, it is simpler to "return N;" instead of "std::extent< T[ N ] >::value;". – prgDevelop Feb 14 '14 at 09:31
  • That's pretty - I use this often enough that a `countof` really should just be part of std library. @Ash What version? MSVC 2013 returns 6 for countof("Hello") rather than 0. – Dwayne Robinson Nov 02 '14 at 09:04
  • 2
    curious if 'return N' is a simpler way of implementing template countof() – MT-Wizard Jan 11 '19 at 00:08
13

Update: C++ 17 support std::size() (defined in header <iterator>)

You can use boost::size() instead:

#include <boost/range.hpp>

int my_array[10];
boost::size(my_array);
KindDragon
  • 6,558
  • 4
  • 47
  • 75
  • 3
    I just love it when people offer boost solutions for even the most trivial one-liner questions totally disregarding that if someone is not already using boost (and there are perfectly valid reasons for not using it), they are supposed to pull in 300+ MB library just for that one thing. – Igor Levicki Sep 01 '20 at 13:22
  • Only issue is that std::size returns size_t which may need casting when callee has a different prototype (e.g. in Win32 buffer sizes are often defined as int). – gast128 Jun 30 '21 at 07:23
10

I'm not aware of one for GCC, but Linux uses GCC's __builtin_types_compatible_p builtin to make their ARRAY_SIZE() macro safer (it'll cause a build break if applied to a pointer):

/* &a[0] degrades to a pointer: a different type from an array */
#define __must_be_array(a) \
 BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))

Note: I think the BUILD_BUG_ON_ZERO() macro has a misleading name (it causes a build failure if the expression is not zero and returns 0 otherwise):

/* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))

I think the naming for this macro comes from looking at it in two parts: BUILD_BUG_ON is what the macro does when the expression is true, and ZERO is the value 'returned' by the macro (if there's not a build break).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
8

This?

#define _countof(a) (sizeof(a)/sizeof(*(a)))

Mud
  • 28,277
  • 11
  • 59
  • 92
  • 7
    that would be unsafe when a is a pointer (vs. array). the macro _countof is safe. – Uri London Jan 07 '13 at 09:59
  • 7
    @Uri only in C++. "In C, _countof will produce erroneous results if array is a pointer." - https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms175773(v=vs.100) – Tom Lint May 30 '18 at 09:45