Why does the C standard not mention any UB in its definition of offsetof()? Is it implicit somewhere else that doing offsetof(struct foo, arr[SIZE_MAX]) on a struct containing a flexible array member is UB?

Both GCC and clang don't diagnose anything here and compile it to a "return 0":

#include <stddef.h>
#include <inttypes.h>
struct foo {
int a;
int arr[];
};
unsigned long func(void) {
return offsetof(struct foo, arr[SIZE_MAX]);
}

godbolt.org/z/rx7rqvh7j

Follow

@jann

Do you mean that it's UB, that the result is an undefined value, or that the result is implementation-defined (and potentially not consistent across multiple different calls to offsetof)?

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.