Skip to content
Browse files

ARRAY_SIZE(): make it usable on 64-bit systems

With code that looks like this:

	for (int i = ARRAY_SIZfoo) - 1; i >= 0; i--) ...

If foo is empty, ARRAY_SIZfoo) will return 0. But since it is
implemented using an unsigned long, the answer to 0UL - 1 is
18446744073709551615 on a 64-bit system, and that doesn't fit into
an int. The compiler complains with:

warning: overflow in conversion from ‘long unsigned int’ to ‘int’ changes value from ‘18446744073709551615’ to ‘-1’ [-Woverflow]

Let's fix that and get the expected behavior simply by turning the
unsigned long into a signed long.

Signed-off-by: Nicolas Pitre <>
  • Loading branch information...
Nicolas Pitre authored and nashif committed May 21, 2019
1 parent a04a2ca commit c2d38ec2b94fe645a29cddf57806a68f9b22589c
Showing with 1 addition and 2 deletions.
  1. +1 −2 include/misc/util.h
@@ -58,8 +58,7 @@ constexpr size_t ARRAY_SIZE(T(&)[N]) { return N; }
* an array (e.g. pointer)
#define ARRAY_SIZE(array) \
((unsigned long) (IS_ARRAY(array) + \
(sizeof(array) / sizeof((array)[0]))))
((long) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))

/* Evaluates to 1 if ptr is part of array, 0 otherwise; compile error if

0 comments on commit c2d38ec

Please sign in to comment.
You can’t perform that action at this time.