Re: offsetof() in array sizes

Carles Cufi

Hi Andrew,

-----Original Message-----
From: Boie, Andrew P [mailto:andrew.p.boie(a)]
Sent: Tuesday, August 23, 2016 16:54
To: Cufi, Carles <Carles.Cufi(a)>;
Subject: RE: offsetof() in array sizes

If one includes a offsetof() statement inside a macro that is then
used to size an array, the following warning is issued by GCC:

warning: variably modified <VAR_NAME> at file scope

The fix for this, at least for GCC, is to use:

#if defined(__GNUC__)
#define offsetof(s, m) __builtin_offsetof(s, m) #else #define
offsetof(s, m) ((size_t)(&(((s *)0)->m))) #endif

Any thoughts about this particular matter? We'd like to use offsetof()
when sizing arrays in the BLE Controller but we don't want to
duplicate the
offsetof() macro.
I don't see a problem with replacing the existing definition in the
minimal libc with the corrected version above.
Can you send a patch against lib/libc/minimal/include/stddef.h?

Thanks for the reply, here's a patch below.



From ddd980e2418053ae346f25deb8ae029f50cb20b3 Mon Sep 17 00:00:00 2001
From: Carles Cufi <carles.cufi(a)>
Subject: [PATCH 1/1] lib: Use offsetof() builtin with GCC

The default offsetof() implementation generates a warning
(variably modified <variable> at file scope) when used in
to define the size of an array. By using this builtin with
GCC we avoid the warning and make sure no variable-size
arrays are used.

Change-Id: Iae215f777241f7daaa061067230086dffaa8311d
Signed-off-by: Carles Cufi <carles.cufi(a)>
lib/libc/minimal/include/stddef.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/lib/libc/minimal/include/stddef.h b/lib/libc/minimal/include/stddef.h
index 2e1ef66..bcc0a6e 100644
--- a/lib/libc/minimal/include/stddef.h
+++ b/lib/libc/minimal/include/stddef.h
@@ -27,6 +27,10 @@
typedef int ptrdiff_t;

+#if defined(__GNUC__)
+#define offsetof(type, member) __builtin_offsetof(type, member)
#define offsetof(type, member) ((size_t) (&((type *) NULL)->member))

#endif /* __INC_stddef_h__ */

Join { to automatically receive all group messages.