Browse Source

AP_Common: simplify ARRAY_SIZE

This rolls back to the simpler version of ARRAY_SIZE. The more complex
one helps catching bugs when we use pointers when we are expecting an
array, but can't stand arrays with 0 elements.  I'm not aware of bugs it
actually caught on ArduPilot, although it did for me in other projects.

I think this is better than having a separate "_SIMPLE" version of the
macro and spread its usage... the trend is just to use the simpler
version anyway.
master
Lucas De Marchi 7 years ago committed by Andrew Tridgell
parent
commit
57ee0e29f6
  1. 8
      libraries/AP_Common/AP_Common.h

8
libraries/AP_Common/AP_Common.h

@ -71,13 +71,7 @@ @@ -71,13 +71,7 @@
#define LOWBYTE(i) ((uint8_t)(i))
#define HIGHBYTE(i) ((uint8_t)(((uint16_t)(i))>>8))
template <typename T, size_t N>
char (&_ARRAY_SIZE_HELPER(T (&_arr)[N]))[N];
template <typename T>
char (&_ARRAY_SIZE_HELPER(T (&_arr)[0]))[0];
#define ARRAY_SIZE(_arr) sizeof(_ARRAY_SIZE_HELPER(_arr))
#define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0]))
// simpler ARRAY_SIZE which can handle zero elements
#define ARRAY_SIZE_SIMPLE(_arr) (sizeof(_arr)/sizeof(_arr[0]))

Loading…
Cancel
Save