问题
Is ARRAY_SIZE return undefined behaviour when the array is empty? because we make a devide of unexisting sizeof((X)[0])
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(X) sizeof((X))/sizeof((X)[0])
#endif
struct ka {
int a;
int b;
};
int main(void)
{
struct ka k[] = {};
printf("%d\n", ARRAY_SIZE(k));
}
回答1:
It is not possible to have zero-sized arrays in Standard C or C++.
In C your code is a constraint violation (empty initializer lists are not permitted anywhere).
In C++ it is also an error; {}
may not be used with an array definition that omits the size. (C++14 [dcl.init.aggr]/4)
If you use a non-standard compiler extension then the behaviour will depend on the details of that extension.
回答2:
In general, from the point of view for memory access, this is fine, because, unless the operand of sizeof
is of VLA type, they are not evaluated. So, in this case, x[0]
is not an invalid memory access.
Quoting C11
, chapter §6.5.3.4, emphasis mine
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.
In a broad sense, for an array like
int arr[5]= {0};
writing
sizeof(arr)/sizeof(arr[10]);
is also valid as arr[10]
is not being evaluated, it's only about the size of the operand, not the content (so, needs no dereferencing).
That said,
- zero-length arrays are not standard C, they are gcc extension.
sizeof
yields a results of sizesize_t
, so we should use%zu
format specifier to print the result.
来源:https://stackoverflow.com/questions/42341110/does-array-size-return-undefined-behaviour-when-the-array-is-empty