• <tfoot id='OXcUO'></tfoot>

    • <bdo id='OXcUO'></bdo><ul id='OXcUO'></ul>

    1. <i id='OXcUO'><tr id='OXcUO'><dt id='OXcUO'><q id='OXcUO'><span id='OXcUO'><b id='OXcUO'><form id='OXcUO'><ins id='OXcUO'></ins><ul id='OXcUO'></ul><sub id='OXcUO'></sub></form><legend id='OXcUO'></legend><bdo id='OXcUO'><pre id='OXcUO'><center id='OXcUO'></center></pre></bdo></b><th id='OXcUO'></th></span></q></dt></tr></i><div id='OXcUO'><tfoot id='OXcUO'></tfoot><dl id='OXcUO'><fieldset id='OXcUO'></fieldset></dl></div>
    2. <legend id='OXcUO'><style id='OXcUO'><dir id='OXcUO'><q id='OXcUO'></q></dir></style></legend>

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

        为什么 NULL/0 是对象的非法内存位置?

        Why is NULL/0 an illegal memory location for an object?(为什么 NULL/0 是对象的非法内存位置?)
          <tbody id='IZdYD'></tbody>

        1. <small id='IZdYD'></small><noframes id='IZdYD'>

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

                • <legend id='IZdYD'><style id='IZdYD'><dir id='IZdYD'><q id='IZdYD'></q></dir></style></legend>
                  本文介绍了为什么 NULL/0 是对象的非法内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我了解 C/C++ 中 NULL 常量的用途,并且我了解它需要在内部以某种方式表示.

                  I understand the purpose of the NULL constant in C/C++, and I understand that it needs to be represented some way internally.

                  我的问题是:对于 C/C++ 中的对象来说,0 地址是无效的内存位置是否有一些根本原因?或者我们是否理论上因为这个保留而浪费"了一个字节的内存?

                  My question is: Is there some fundamental reason why the 0-address would be an invalid memory-location for an object in C/C++? Or are we in theory "wasting" one byte of memory due to this reservation?

                  推荐答案

                  空指针实际上不必为 0.在 C 规范中保证,当在指针的上下文中给出常量 0 值时,它是编译器将其视为 null,但是如果您这样做

                  The null pointer does not actually have to be 0. It's guaranteed in the C spec that when a constant 0 value is given in the context of a pointer it is treated as null by the compiler, however if you do

                  char *foo = (void *)1;
                  --foo;
                  // do something with foo
                  

                  您将访问 0 地址,不一定是空指针.在大多数情况下,实际情况确实如此,但这不是必需的,因此我们实际上不必浪费那个字节.虽然,在更大的图片中,如果它不是 0,它必须是某个东西,所以一个字节被浪费了某处

                  You will access the 0-address, not necessarily the null pointer. In most cases this happens to actually be the case, but it's not necessary, so we don't really have to waste that byte. Although, in the larger picture, if it isn't 0, it has to be something, so a byte is being wasted somewhere

                  由于注释中的混淆,删除了 NULL 的使用.此外,这里的主要信息是空指针!= 0,这里有一些 C/伪代码,显示了我试图提出的观点."请不要实际尝试编译它或担心类型是否正确;意思很明确.

                  Edited out the use of NULL due to the confusion in the comments. Also, the main message here is "null pointer != 0, and here's some C/pseudo code that shows the point I'm trying to make." Please don't actually try to compile this or worry about whether the types are proper; the meaning is clear.

                  这篇关于为什么 NULL/0 是对象的非法内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  <tfoot id='6InFE'></tfoot>

                    • <i id='6InFE'><tr id='6InFE'><dt id='6InFE'><q id='6InFE'><span id='6InFE'><b id='6InFE'><form id='6InFE'><ins id='6InFE'></ins><ul id='6InFE'></ul><sub id='6InFE'></sub></form><legend id='6InFE'></legend><bdo id='6InFE'><pre id='6InFE'><center id='6InFE'></center></pre></bdo></b><th id='6InFE'></th></span></q></dt></tr></i><div id='6InFE'><tfoot id='6InFE'></tfoot><dl id='6InFE'><fieldset id='6InFE'></fieldset></dl></div>

                      1. <small id='6InFE'></small><noframes id='6InFE'>

                        <legend id='6InFE'><style id='6InFE'><dir id='6InFE'><q id='6InFE'></q></dir></style></legend>

                          • <bdo id='6InFE'></bdo><ul id='6InFE'></ul>
                              <tbody id='6InFE'></tbody>