问题描述
使用%p
转换说明符打印空指针是否是未定义的行为?
Is it undefined behavior to print null pointers with the %p
conversion specifier?
#include <stdio.h>
int main(void) {
void *p = NULL;
printf("%p", p);
return 0;
}
该问题适用于C标准,不适用于C实现.
The question applies to the C standard, and not to C implementations.
推荐答案
这是一些奇怪的案例,其中我们受到英语语言的限制以及标准中结构不一致的限制.因此充其量,我可以提出一个令人反感的论点,因为不可能证明:)
问题中的代码表现出明确的行为.
The code in the question exhibits well-defined behaviour.
由于 [7.1.4] 是问题的基础,所以从这里开始:
As [7.1.4] is the basis of the question, let's start there:
这是笨拙的语言.一种解释是,对于所有库功能,列表中的项目均为UB,除非单独的说明将其覆盖.但是该列表以诸如"开头,表示它是说明性的,并非详尽无遗.例如,它没有提到正确的字符串空终止(对于例如strcpy
的行为至关重要).
This is clumsy language. One interpretation is that the items in the list are UB for all library functions, unless overridden by the individual descriptions. But the list starts with "such as", indicating that it's illustrative, not exhaustive. For example, it does not mention correct null-termination of strings (critical for the behaviour of e.g. strcpy
).
因此,很明显,7.1.4的意图/范围只是无效值"导致了UB(除非另有说明).我们必须查看每个函数的描述,以确定哪些算作无效值".
Thus it's clear the intent/scope of 7.1.4 is simply that an "invalid value" leads to UB (unless stated otherwise). We have to look to each function's description to determine what counts as an "invalid value".
[7.21.2.3] 仅说:
它没有明确提及空指针,但是也没有提及空终止符.取而代之的是,从"s2
指向的字符串"中推断出唯一有效的值是字符串(即,指向以null终止的字符数组的指针).
It makes no explicit mention of null pointers, yet it makes no mention of null terminators either. Instead, one infers from "string pointed to by s2
" that the only valid values are strings (i.e. pointers to null-terminated character arrays).
实际上,可以在各个描述中看到这种模式.其他示例:
Indeed, this pattern can be seen throughout the individual descriptions. Some other examples:
[7.19.6.1] 谈到%p
:
Null是一个有效的指针值,本节没有明确提到null是一种特殊情况,也没有指出指针必须指向一个对象.这样就定义了行为.
Null is a valid pointer value, and this section makes no explicit mention that null is a special case, nor that the pointer has to point at an object. Thus it is defined behaviour.
这篇关于用%p打印空指针是未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!