<tfoot id='ThCby'></tfoot>
    <legend id='ThCby'><style id='ThCby'><dir id='ThCby'><q id='ThCby'></q></dir></style></legend>
    1. <small id='ThCby'></small><noframes id='ThCby'>

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

        0xDEADBEEF 与 NULL

        0xDEADBEEF vs. NULL(0xDEADBEEF 与 NULL)
      1. <small id='8U99a'></small><noframes id='8U99a'>

          <legend id='8U99a'><style id='8U99a'><dir id='8U99a'><q id='8U99a'></q></dir></style></legend>

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

                  本文介绍了0xDEADBEEF 与 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在各种代码中,我已经看到调试版本中的内存分配带有 NULL...

                  Throughout various code, I have seen memory allocation in debug builds with NULL...

                  memset(ptr,NULL,size);
                  

                  或者使用 0xDEADBEEF...

                  memset(ptr,0xDEADBEEF,size);
                  

                  1. 使用每种方法的优点是什么,在 C/C++ 中实现这一点的一般首选方法是什么?
                  2. 如果一个指针被分配了 0xDEADBEEF 的值,它是否仍然服从有效数据?
                  1. What are the advantages to using each one, and what is the generally preferred way to achieve this in C/C++?
                  2. If a pointer was assigned a value of 0xDEADBEEF, couldn't it still deference to valid data?

                  推荐答案

                  1. 使用 memset(ptr, NULL, size)memset(ptr, 0xDEADBEEF, size) 清楚地表明了作者这样做的事实不明白他们在做什么.

                  1. Using either memset(ptr, NULL, size) or memset(ptr, 0xDEADBEEF, size) is a clear indication of the fact that the author did not understand what they were doing.

                  首先,如果 NULL 定义为整数零,memset(ptr, NULL, size) 确实会将 C 和 C++ 中的内存块清零.

                  Firstly, memset(ptr, NULL, size) will indeed zero-out a memory block in C and C++ if NULL is defined as an integral zero.

                  然而,在此上下文中使用 NULL 表示零值是不可接受的做法.NULL 是一个专门为指针上下文引入的宏.memset 的第二个参数是一个整数,而不是一个指针.将内存块清零的正确方法是 memset(ptr, 0, size).注意:0 不是 NULL.我会说即使 memset(ptr, '', size) 看起来也比 memset(ptr, NULL, size) 好.

                  However, using NULL to represent the zero value in this context is not an acceptable practice. NULL is a macro introduced specifically for pointer contexts. The second parameter of memset is an integer, not a pointer. The proper way to zero-out a memory block would be memset(ptr, 0, size). Note: 0 not NULL. I'd say that even memset(ptr, '', size) looks better than memset(ptr, NULL, size).

                  此外,最新的(目前)C++ 标准 - C++11 - 允许将 NULL 定义为 nullptr.nullptr value 不能隐式转换为 int 类型,这意味着上面的代码不能保证在 C++11 及更高版本中编译.

                  Moreover, the most recent (at the moment) C++ standard - C++11 - allows defining NULL as nullptr. nullptr value is not implicitly convertible to type int, which means that the above code is not guaranteed to compile in C++11 and later.

                  在 C 语言中(你的问题也被标记为 C)宏 NULL 可以扩展为 (void *) 0.即使在 C 中 (void *) 0 也不能隐式转换为 int 类型,这意味着在一般情况下 memset(ptr, NULL, size) 只是 C 中的无效代码.

                  In C language (and your question is tagged C as well) macro NULL can expand to (void *) 0. Even in C (void *) 0 is not implicitly convertible to type int, which means that in general case memset(ptr, NULL, size) is simply invalid code in C.

                  其次,即使 memset 的第二个参数的类型为 int,该函数仍将其解释为 unsigned char 值.这意味着仅使用该值的一个低字节来填充目标内存块.出于这个原因,memset(ptr, 0xDEADBEEF, size) 将编译,但不会用 0xDEADBEEF 值填充目标内存区域,正如代码的作者可能天真地希望的那样.memset(ptr, 0xDEADBEEF, size) 等价于 memset(ptr, 0xEF, size)(假设为 8 位字符).虽然这可能足以用故意的垃圾"填充一些内存区域,但诸如 memset(ptr, NULL, size)memset(ptr, 0xDEADBEEF, size)仍然暴露出作者缺乏专业精神.

                  Secondly, even though the second parameter of memset has type int, the function interprets it as an unsigned char value. It means that only one lower byte of the value is used to fill the destination memory block. For this reason memset(ptr, 0xDEADBEEF, size) will compile, but will not fill the target memory region with 0xDEADBEEF values, as the author of the code probably naively hoped. memset(ptr, 0xDEADBEEF, size) is eqivalent to memset(ptr, 0xEF, size) (assuming 8-bit chars). While this is probably good enough to fill some memory region with intentional "garbage", things like memset(ptr, NULL, size) or memset(ptr, 0xDEADBEEF, size) still betray the major lack of professionalism on the author's part.

                  同样,正如其他答案已经指出的那样,这里的想法是用垃圾"值填充未使用的内存.在这种情况下,零肯定不是一个好主意,因为它不够垃圾".使用 memset 时,您只能使用一字节值,例如 0xAB0xEF.如果这足以满足您的目的,请使用 memset.如果您想要一个更具表现力和独特的垃圾值,例如 0xDEDABEEF0xBAADFOOD,您将无法将 memset 与它一起使用.您必须编写一个可以用 4 字节模式填充内存区域的专用函数.

                  Again, as other answer have already noted, the idea here is to fill the unused memory with a "garbage" value. Zero is certainly not a good idea in this case, since it is not "garbagy" enough. When using memset you are limited to one-byte values, like 0xAB or 0xEF. If this is good enough for your purposes, use memset. If you want a more expressive and unique garbage value, like 0xDEDABEEF or 0xBAADFOOD, you won't be able to use memset with it. You'll have to write a dedicated function that can fill memory region with 4-byte pattern.

                  C 和 C++ 中的指针不能分配任意整数值(空指针常量除外,即零).这种分配只能通过使用显式强制转换将整数值强制转换为指针来实现.正式地说,这种类型转换的结果是实现定义的.结果值当然可以指向有效数据.

                  A pointer in C and C++ cannot be assigned an arbitrary integer value (other than a Null Pointer Constant, i.e. zero). Such assignment can only be achieved by forcing the integral value into the pointer with an explicit cast. Formally speaking, the result of such a cast is implementation defined. The resultant value can certainly point to valid data.

                  这篇关于0xDEADBEEF 与 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)
                    <tbody id='2XJ36'></tbody>
                1. <tfoot id='2XJ36'></tfoot>

                        <small id='2XJ36'></small><noframes id='2XJ36'>

                            <bdo id='2XJ36'></bdo><ul id='2XJ36'></ul>

                          • <legend id='2XJ36'><style id='2XJ36'><dir id='2XJ36'><q id='2XJ36'></q></dir></style></legend>

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