- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在所有未定义行为的列表中引用 ANSI_ISO+9899-1990 -
- An identifier is used that is not visible in the current scope
为什么使用 undefined variable UB 而不是错误?我想知道这背后的原因?将所有不“正确”的内容都作为 UB 有效吗?
我知道大多数编译器将其视为错误。但是为什么标准会这样说呢?
或者标准中没有“错误”的概念?c的文法是.* 但有一部分有UB?
最佳答案
不,标准中有很多错误的概念,只是不是所有的东西都算作错误。我会先说两件事,这可能对您有所帮助:
C89/90 是编纂现有实践,不是创造一种新语言。这意味着有时有必要在事情上妥协,即使它们没有意义。
C99+ 没有该限制,因此可以清除某些内容。事实上,由于您引用的那段文字似乎在 C99 中消失了,这很可能是清理的内容之一。
回到推理,ANSI C89 基本原理文档是这样说的:
One source of dispute was whether identifiers with external linkage should have file scope even when introduced within a block. The Base Document is vague on this point, and has been interpreted differently by different implementations. For example, the following fragment would be valid in the file scope scheme, while invalid in the block scope scheme:
typedef struct data d_struct ;
first(){
extern d_struct func();
/* ... */
}
second(){
d_struct n = func();
}
While it was generally agreed that it is poor practice to take advantage of an external declaration once it had gone out of scope, some argued that a translator had to remember the declaration for checking anyway, so why not acknowledge this? The compromise adopted was to decree essentially that block scope rules apply, but that a conforming implementation need not diagnose a failure to redeclare an external identifier that had gone out of scope (undefined behavior).
几乎可以肯定的是,“已被不同的实现以不同方式解释”这句话导致他们将其标记为 UB 而不是错误。请注意,这里讨论的不是范围,而是实现是否需要报告您正在尝试使用它的事实。
关于c - 为什么使用不在 UB 范围内的标识符而不是错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42546831/
在回答中 Is it valid to create closure (lambda) objects using `std::bit_cast` in C++20? 表明程序可能具有未定义的行为“取
C++ 20 draft [concept.default.init] 没有精确定义 default_initializable template concept default_initializa
我担心这又是一个关于解释 ISO/IEC 14882(C++ 标准)的问题,但是: 正在从程序中调用 main,例如我从 main 递归调用 main() 至少不是实现定义的行为? (更新:我的意思是
想象一下: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); 我有 rea
考虑以下代码: int main() { typedef struct { int first; float second; } type; type whole = { 1, 2.0
这个问题已经有答案了: (Why) is using an uninitialized variable undefined behavior? (7 个回答) 已关闭 6 年前。 SO 上各种受人尊
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我最近在这样的代码中遇到了一个错误 class C { public: // foo return value depends on C's state // AND each cal
我最近在升级到 gcc 7.2 后遇到了一个奇怪的 C++ 崩溃,这可以使用以下简单的完整 c++11 程序来演示: #include #include struct MyObject {
所以,我一直在阅读 the C++ standard并来到 [defns.undefined](我正在阅读的 C++17 草案中的 3.27。请注意,虽然我在这里引用 C++17,但我在 other
这不是 Rvalue reference to lvalue reference 的拷贝 由于我是针对 C++17 提出这个问题的:由于悬空引用,以下是否仍会调用未定义的行为(当然,如果使用了引用)?
这个问题在这里已经有了答案: Arithmetic right shift gives bogus result? (2 个答案) 关闭 7 年前。 我想检查一些大的计算内存需求(存储在 unsig
坦率地说,这样的代码是否有效(除了缺乏必要的错误检查,为简单起见此处省略)? 通过互联网发送数据的代码: uint16_t i = htons(500); sendto(sockfd, &i, siz
我知道 C 和 C++ 以及不同的语言,但以下内容适用于两者。 TL/DR 我知道 i = i++; 是 UB,因为 i 在表达式中被修改了两次,而 C 和 C++ 禁止它。 引用资料: C99 6.
是否有可能满足以下条件的代码在每次运行相同输入时产生不同的输出? 虽然代码是单线程的它确实链接到一个线程安全的运行时库。 没有明确调用 rand() 或 time() 或它们的 friend 。 有一
这个问题在这里已经有了答案: is there issue will stringstream.str().c_str()? [duplicate] (3 个答案) 关闭 6 年前。 考虑以下代码,
根据 C++ 标准,如果对象本身不是 const,则可以从指针中丢弃 const 并写入对象。这样: const Type* object = new Type(); const_cast( ob
下面的代码会调用 UB 吗? int main(){ volatile int i = 0; volatile int* p = &i; int j = ++i * *p; } 最佳答案
这个问题在这里已经有了答案: will casting around sockaddr_storage and sockaddr_in break strict aliasing (2 个答案) 关
假设我们有来自 C++98 的遗留代码: bool expensiveCheck(); struct Foo; bool someFunc() { Foo *ptr = 0; if(
我是一名优秀的程序员,十分优秀!