问题描述
首先,我看到了,并且接受的答案参考操作数不是在C99标准草案中评估了字样。我不知道这个答案适用于C ++ 03。还有,它有一个接受的答案,引用了类似的措辞和在一些上下文中,未评估的操作数出现。未评估的操作数未评估。
我有此代码:
int * ptr = 0;
void * buffer = malloc(10 * sizeof(* ptr));
问题是 - 是否有空指针解引用(因此UB) sizeof()
?
C ++ 03 5.3.3 / 1说 sizeof运算符产生字节数其操作数的对象表示。操作数是未计算的表达式,或带括号的类型ID。
链接到答案的引用或类似的措辞但是我找不到标准链接到评估的具体位置是否具有或不具有UB。
是否不评估应用sizeof的表达式是否合法,在C ++中的sizeof内引用空指针或无效指针?
我认为这在标准中目前规定不足,像许多问题一样,例如:
noexcept(*(U *)0 = declval U())
建议被拒绝,因为它没有调用未定义的行为,因为它是未评估的:
这个理由适用于 sizeof $
我说是underpecified,但我不知道这是否包含在 4.1
[conv.lval] 这说明:
它表示不包含的值,如果我们遵循表示没有未定义的行为:
and suggested an alternate expression, it refers to this expression which is no longer in the standard but can be found in N3090:
noexcept(*(U*)0 = declval<U>())
The suggestion was rejected since this does not invoke undefined behavior since it is unevaluated:
This rationale applies to sizeof
as well since it's operands are unevaluated.
I say underspecified but I wonder if this is covered by section 4.1
[conv.lval] which says:
It says the value contained is not accessed, which if we follow the logic of issue 232 means there is no undefined behavior:
This is somewhat speculative since the issue is not settled yet.
这篇关于不评估应用sizeof的表达式是否合法,在C ++中解析sizeof中的空指针或无效指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!