CAN"的sizeof(ARR [0])QUOT;导致未定义行为? [英] Can "sizeof(arr[0])" lead to undefined behavior?
问题描述
有就是要弄清楚数组长度的一个众所周知的模式:
There is a well known pattern of figuring out array length:
int arr[10];
size_t len = sizeof(arr) / sizeof(arr[0]);
assert(len == 10);
此模式适用于静态数组和恒定的尺寸自动阵列。它也适用于在C99中可变长度阵列。
This pattern applies to static arrays and auto arrays of constant size. It also applies to variable length arrays in C99.
我想申请类似的想法以字节为单位计算出的动态数组的大小:
I want to apply similar idea for figuring out dynamic array size in bytes:
size_t known_len = 10;
int *ptr = malloc(known_len * sizeof(int));
size_t size = known_len * sizeof(ptr[0]);
assert(size == known_len * sizeof(int));
这是不是 known_len *的sizeof(INT)
,因为的sizeof(PTR [0])
不漂亮参考实际的数组元素类型。因此,它不需要$ C $的c读者了解的类型。
This is nicer than known_len * sizeof(int)
because sizeof(ptr[0])
doesn't refer to actual array element type. Hence it doesn't require reader of the code to know the type.
不过,目前尚不清楚也好,我前pression 的sizeof(PTR [0])
可能会导致不确定的行为。由于扩展:
However it is unclear to me whether expression sizeof(ptr[0])
can lead to undefined behavior. As it is expanded:
sizeof(ptr[0]) -> sizeof(*((ptr) + (0))) -> sizeof(*ptr)
结果前pression是值得怀疑的情况下,如果 PTR
是 0
:
sizeof(*((int*) 0))
根据C99标准:
According to a C99 standard:
(C99,6.3.2.3p3):整型常量前pression与价值 0
,
或者这样一个前pression投键入无效*
,被称为空指针
不变的。提领一空指针是未定义的行为。
(C99, 6.3.2.3p3 ): "An integer constant expression with the value
0
, or such an expression cast to typevoid *
, is called a null pointer constant." Dereferencing a null pointer is undefined behavior.
(C99,6.5.3.2.p4)如果无效值已被分配给该
指针,一元的行为*
运算符是undefined.87)
(C99, 6.5.3.2.p4) "If an invalid value has been assigned to the
pointer, the behavior of the unary *
operator is undefined.87)"
87):在由解引用指针无效值
一元 *
操作符是一个空指针,地址不适当
为对象的类型对齐指出,和一个地址
其生命周期结束后的对象。
87): "Among the invalid values for dereferencing a pointer by the
unary *
operator are a null pointer, an address inappropriately
aligned for the type of object pointed to, and the address of an
object after the end of its lifetime."
但是它从来没有指明的前pression的sizeof的是否会导致不确定的行为。其实这样的sizeof应该在编译时进行评估。
But it's never specified whether sizeof of such expression can lead to undefined behavior. In fact such sizeof should be evaluated at compile time.
我的问题是:
- 可以除权pression
的sizeof(PTR [0])
在code时使用类型的PTR
是已知的,PTR
不知道? - 可以这样使用可根据C99标准是否合理? GNU GCC规范呢?
- Can the expression
sizeof(ptr[0])
be used in the code when type ofptr
is known and the value ofptr
is not known? - Can such use be justified according to C99 standard? GNU GCC spec?
推荐答案
这位前pression PTR [0]
不会被评估的sizeof(PTR [0])
。大小将仅使用 PTR [0]
的类型在编译时确定。
The expression ptr[0]
will not be evaluated in sizeof(ptr[0])
. Size will be determined by just using the type of ptr[0]
at compile time.
的的sizeof
运营商产生的操作数的大小(以字节为单位),这可能是一个前pression或类型的括号名称。 的大小是从操作数的类型确定。结果是整数。 如果操作数的类型是可变长度数组类型,操作数被评估;否则,操作数不计算,结果是aninteger不变。
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 aninteger constant.
这意味着,没有任何不确定的行为。
That means, there is no undefined behavior.
这篇关于CAN"的sizeof(ARR [0])QUOT;导致未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!