<bdo id='9ew8q'></bdo><ul id='9ew8q'></ul>
    <legend id='9ew8q'><style id='9ew8q'><dir id='9ew8q'><q id='9ew8q'></q></dir></style></legend>

    1. <small id='9ew8q'></small><noframes id='9ew8q'>

      <tfoot id='9ew8q'></tfoot>
      <i id='9ew8q'><tr id='9ew8q'><dt id='9ew8q'><q id='9ew8q'><span id='9ew8q'><b id='9ew8q'><form id='9ew8q'><ins id='9ew8q'></ins><ul id='9ew8q'></ul><sub id='9ew8q'></sub></form><legend id='9ew8q'></legend><bdo id='9ew8q'><pre id='9ew8q'><center id='9ew8q'></center></pre></bdo></b><th id='9ew8q'></th></span></q></dt></tr></i><div id='9ew8q'><tfoot id='9ew8q'></tfoot><dl id='9ew8q'><fieldset id='9ew8q'></fieldset></dl></div>
    2. 为什么增强的 GCC 6 优化器会破坏实用的 C++ 代码

      时间:2023-09-19
    3. <i id='HOGgS'><tr id='HOGgS'><dt id='HOGgS'><q id='HOGgS'><span id='HOGgS'><b id='HOGgS'><form id='HOGgS'><ins id='HOGgS'></ins><ul id='HOGgS'></ul><sub id='HOGgS'></sub></form><legend id='HOGgS'></legend><bdo id='HOGgS'><pre id='HOGgS'><center id='HOGgS'></center></pre></bdo></b><th id='HOGgS'></th></span></q></dt></tr></i><div id='HOGgS'><tfoot id='HOGgS'></tfoot><dl id='HOGgS'><fieldset id='HOGgS'></fieldset></dl></div>

        <tfoot id='HOGgS'></tfoot><legend id='HOGgS'><style id='HOGgS'><dir id='HOGgS'><q id='HOGgS'></q></dir></style></legend>

          <small id='HOGgS'></small><noframes id='HOGgS'>

            <bdo id='HOGgS'></bdo><ul id='HOGgS'></ul>
                  <tbody id='HOGgS'></tbody>
                本文介绍了为什么增强的 GCC 6 优化器会破坏实用的 C++ 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                GCC 6 有一个新的优化器功能:它假设 this始终不为 null 并基于此进行优化.

                GCC 6 has a new optimizer feature: It assumes that this is always not null and optimizes based on that.

                值范围传播现在假定 C++ 成员函数的 this 指针为非空.这消除了常见的空指针检查但也破坏了一些不符合规范的代码库(例如 Qt-5、Chromium、KDevelop).可以使用 -fno-delete-null-pointer-checks 作为临时解决方法.可以使用 -fsanitize=undefined 来识别错误代码.

                Value range propagation now assumes that the this pointer of C++ member functions is non-null. This eliminates common null pointer checks but also breaks some non-conforming code-bases (such as Qt-5, Chromium, KDevelop). As a temporary work-around -fno-delete-null-pointer-checks can be used. Wrong code can be identified by using -fsanitize=undefined.

                变更文档明确指出这是危险的,因为它破坏了大量常用代码.

                The change document clearly calls this out as dangerous because it breaks a surprising amount of frequently used code.

                为什么这个新假设会破坏实际的 C++ 代码? 是否有一些特定的模式让粗心或不知情的程序员依赖于这种特定的未定义行为?我无法想象有人会写 if (this == NULL) 因为那太不自然了.

                Why would this new assumption break practical C++ code? Are there particular patterns where careless or uninformed programmers rely on this particular undefined behavior? I cannot imagine anyone writing if (this == NULL) because that is so unnatural.

                推荐答案

                我想需要回答的问题是为什么好心的人会首先写支票.

                I guess the question that needs to be answered why well-intentioned people would write the checks in the first place.

                最常见的情况可能是,如果您的类是自然发生的递归调用的一部分.

                The most common case is probably if you have a class that is part of a naturally occurring recursive call.

                如果你有:

                struct Node
                {
                    Node* left;
                    Node* right;
                };
                

                在 C 中,你可以这样写:

                in C, you might write:

                void traverse_in_order(Node* n) {
                    if(!n) return;
                    traverse_in_order(n->left);
                    process(n);
                    traverse_in_order(n->right);
                }
                

                在 C++ 中,将其设为成员函数很好:

                In C++, it's nice to make this a member function:

                void Node::traverse_in_order() {
                    // <--- What check should be put here?
                    left->traverse_in_order();
                    process();
                    right->traverse_in_order();
                }
                

                在 C++ 的早期(标准化之前),人们强调成员函数是函数的语法糖,其中 this 参数是隐式的.代码是用 C++ 编写的,转换为等效的 C 并编译.甚至有明确的例子表明,将 this 与 null 进行比较是有意义的,原始的 Cfront 编译器也利用了这一点.所以来自 C 背景,检查的明显选择是:

                In the early days of C++ (prior to standardization), it was emphasized that that member functions were syntactic sugar for a function where the this parameter is implicit. Code was written in C++, converted to equivalent C and compiled. There were even explicit examples that comparing this to null was meaningful and the original Cfront compiler took advantage of this too. So coming from a C background, the obvious choice for the check is:

                if(this == nullptr) return;      
                

                注意:Bjarne Stroustrup 甚至提到 this 的规则多年来发生了变化 这里

                Note: Bjarne Stroustrup even mentions that the rules for this have changed over the years here

                这在许多编译器上工作了很多年.当标准化发生时,情况发生了变化.最近,编译器开始利用调用成员函数的优势,其中 thisnullptr 是未定义的行为,这意味着此条件始终为 false,编译器可以随意省略它.

                And this worked on many compilers for many years. When standardization happened, this changed. And more recently, compilers started taking advantage of calling a member function where this being nullptr is undefined behavior, which means that this condition is always false, and the compiler is free to omit it.

                这意味着要对该树进行任何遍历,您需要:

                That means that to do any traversal of this tree, you need to either:

                • 在调用traverse_in_order

                void Node::traverse_in_order() {
                    if(left) left->traverse_in_order();
                    process();
                    if(right) right->traverse_in_order();
                }
                

                这意味着还要检查每个呼叫站点是否有空根.

                This means also checking at EVERY call site if you could have a null root.

                不要使用成员函数

                这意味着您正在编写旧的 C 样式代码(可能作为静态方法),并使用对象作为参数显式调用它.例如.你又回到了在调用站点编写 Node::traverse_in_order(node); 而不是 node->traverse_in_order();.

                This means that you're writing the old C style code (perhaps as a static method), and calling it with the object explicitly as a parameter. eg. you're back to writing Node::traverse_in_order(node); rather than node->traverse_in_order(); at the call site.

                我认为以符合标准的方式修复这个特定示例的最简单/最简洁的方法是实际使用哨兵节点而不是 nullptr.

                I believe the easiest/neatest way to fix this particular example in a way that is standards compliant is to actually use a sentinel node rather than a nullptr.

                // static class, or global variable
                Node sentinel;
                
                void Node::traverse_in_order() {
                    if(this == &sentinel) return;
                    ...
                }
                

                前两个选项似乎都没有吸引力,虽然代码可以逃脱,但他们使用 this == nullptr 而不是使用适当的修复编写了糟糕的代码.

                Neither of the first two options seem that appealing, and while code could get away with it, they wrote bad code with this == nullptr instead of using a proper fix.

                我猜这就是其中一些代码库如何演变为在其中包含 this == nullptr 检查.

                I'm guessing that's how some of these code bases evolved to have this == nullptr checks in them.

                这篇关于为什么增强的 GCC 6 优化器会破坏实用的 C++ 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何判断头文件是从哪里包含的? 下一篇:对 `__stack_chk_fail' 的未定义引用

                相关文章

                最新文章

                    • <bdo id='BXZNI'></bdo><ul id='BXZNI'></ul>
                    <i id='BXZNI'><tr id='BXZNI'><dt id='BXZNI'><q id='BXZNI'><span id='BXZNI'><b id='BXZNI'><form id='BXZNI'><ins id='BXZNI'></ins><ul id='BXZNI'></ul><sub id='BXZNI'></sub></form><legend id='BXZNI'></legend><bdo id='BXZNI'><pre id='BXZNI'><center id='BXZNI'></center></pre></bdo></b><th id='BXZNI'></th></span></q></dt></tr></i><div id='BXZNI'><tfoot id='BXZNI'></tfoot><dl id='BXZNI'><fieldset id='BXZNI'></fieldset></dl></div>
                  1. <small id='BXZNI'></small><noframes id='BXZNI'>

                  2. <tfoot id='BXZNI'></tfoot>
                  3. <legend id='BXZNI'><style id='BXZNI'><dir id='BXZNI'><q id='BXZNI'></q></dir></style></legend>