`sizeof ((struct X *)0)->field` — conformance in C1999 specifically?

Specifically in ISO C 1999, not in C++ nor in any newer revision of C1, is it strictly conforming to use sizeof ((struct T *) 0)->field to determine the size of a struct field? For example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stddef.h>
struct thing {
int a;
short b[3];
};
const size_t size_of_thing_b = sizeof ((struct thing *)0)->b; /* this line */
</code>
<code>#include <stddef.h> struct thing { int a; short b[3]; }; const size_t size_of_thing_b = sizeof ((struct thing *)0)->b; /* this line */ </code>
#include <stddef.h>

struct thing {
  int a;
  short b[3];
};

const size_t size_of_thing_b = sizeof ((struct thing *)0)->b;  /* this line */

My initial opinion was that this construct has undefined behavior (and is therefore not strictly conforming). Evidence in favor of that opinion is that the very similar expression &((struct thing *)0)->b definitely does have undefined behavior. In the abstract machine, it dereferences a null pointer. That argument seems like it ought to carry over if & is replaced with sizeof … except that the operand of sizeof EXPR is specifically not evaluated (6.5.3.4p2) (unless there’s a VLA involved, which there isn’t in this case), so the abstract machine doesn’t do anything with it, and I don’t see any other reason why it should have undefined behavior.

Questions about how to get the sizeof a struct field have been asked before (e.g. sizeof single struct member in C) but the answers typically take for granted that sizeof ((struct T *)0)->field has a well-defined result.


1 I am also interested in whether the conformance of this code might have changed since the 1999 specification, either due to wording changes in the normative textpost-1999 or due to DR responses, but only secondarily.

The language of 6.5.3.4p2 is pretty clear on this:

The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand.
The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is an integer constant.

So while an expression such as (struct thing *)0)->b, if evaluated, would invoke undefined behavior due to a NULL pointer dereference, applying sizeof to this expression does not evaluate it, and the expression is looked at just to determine its type.

So sizeof (struct thing *)0)->b is well defined and evaluates to an integer constant.

Also, the text of 6.5.3.4p2 is unchanged in both C11 and C23, so the same applies to those versions of the standard.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật