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:
#define offsetof(s, m) __builtin_offsetof(s, m)
#define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
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.
Boie, Andrew P
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?
Hi Andrew,toggle quoted messageShow quoted text
-----Original Message-----Thanks for the reply, here's a patch below.
From ddd980e2418053ae346f25deb8ae029f50cb20b3 Mon Sep 17 00:00:00 2001
From: Carles Cufi <carles.cufi(a)nordicsemi.no>
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.
Signed-off-by: Carles Cufi <carles.cufi(a)nordicsemi.no>
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
@@ -27,6 +27,10 @@
typedef int ptrdiff_t;
+#define offsetof(type, member) __builtin_offsetof(type, member)
#define offsetof(type, member) ((size_t) (&((type *) NULL)->member))
#endif /* __INC_stddef_h__ */
Boie, Andrew P
Thanks for the reply, here's a patch below.Hi Carles,
Can you submit your contribution to our Gerrit server?