Re:Ill-Advised C++ Rant, Part 2 (2016)
by singingboyo on Wednesday October 25, 2017 @03:56PM
(#15553552)
Still works. Whether X has any elements is irrelevant, as sizeof doesn't evaluate the expression. Instead, it returns the size of the static type of the expression.
an empty struct has a size of 1 byte.
struct Widget{
};
Widget widgets[256];
printf("%d %d", sizeof(widgets), sizeof(Widget));
prints out 256 1
if there are 0 elements, there are no issues because it's 0/x = 0.the point is that they're indexing into an array of unknown size.
>#define countof(X) (sizeof(X) / sizeof((X)[0]))
Could they? What if X is empty?