Path: csiph.com!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: container_of macro... Date: Sat, 19 Nov 2022 06:35:35 -0800 Organization: A noiseless patient Spider Lines: 28 Message-ID: <86iljbc8qw.fsf@linuxsc.com> References: <20210309194126.744@kylheku.com> <9bf38715-21be-4679-9613-2fb682455d78n@googlegroups.com> <87leovszmt.fsf@bsb.me.uk> <75219455-ec54-4d99-9d76-6b123c76172cn@googlegroups.com> <874jvh73di.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="3a600a3efa6b1d635770a4955b3f9787"; logging-data="3411329"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mZW0uMxNf/FucsjFa2yJ2LVh2LTLzhpI=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:xNC2jIvbk07vjXGBa+VzZWTnchU= sha1:626j1EZEUWcHU1nHtQKW42mOvdc= Xref: csiph.com comp.lang.c:168278 Ben Bacarisse writes: > tylo writes: > >> tirsdag 1. november 2022 kl. 12:06:17 UTC+1 skrev Ben Bacarisse: >> >>>> (type*)0->member dereferences a null pointer. As a result, such code has >>>> undefined behavior. >>> >>> It's in the unevaluated operand of the sizeof operator. The comparison >>> is there just to provoke type-checking (the main thrust of this >>> subthread) and I imagine the sizeof is there to avoid the implied UB. >> >> Yes, sizeof is also there to avoid ptr being evaluated twice. >> >> Although not relevant here as sizeof() doesn't evaluate its argument, >> the expression &((Type*)0)->member evaluates to a compile time >> constant even by non-optimizing compilers, as they always do basic >> constant folding optimizations. > > Compilers are permitted to do what they like with that expression (if it > is in a context where it is evaluated) because it the behaviour is > formally undefined. You can's assume anything about the value. Forgive me for making an obvious point here: _shouldn't_ assume anything about the value. People can make unwarranted assumptions about the value, and some people do, even though they shouldn't, and that's the problem.