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

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

    1. <legend id='xZj9j'><style id='xZj9j'><dir id='xZj9j'><q id='xZj9j'></q></dir></style></legend>

          <bdo id='xZj9j'></bdo><ul id='xZj9j'></ul>
      1. memset() 或值初始化以将结构归零?

        时间:2023-10-06
          <tbody id='3Btry'></tbody>
        <i id='3Btry'><tr id='3Btry'><dt id='3Btry'><q id='3Btry'><span id='3Btry'><b id='3Btry'><form id='3Btry'><ins id='3Btry'></ins><ul id='3Btry'></ul><sub id='3Btry'></sub></form><legend id='3Btry'></legend><bdo id='3Btry'><pre id='3Btry'><center id='3Btry'></center></pre></bdo></b><th id='3Btry'></th></span></q></dt></tr></i><div id='3Btry'><tfoot id='3Btry'></tfoot><dl id='3Btry'><fieldset id='3Btry'></fieldset></dl></div>

            <bdo id='3Btry'></bdo><ul id='3Btry'></ul>
          • <tfoot id='3Btry'></tfoot>
          • <legend id='3Btry'><style id='3Btry'><dir id='3Btry'><q id='3Btry'></q></dir></style></legend>

                  <small id='3Btry'></small><noframes id='3Btry'>

                  本文介绍了memset() 或值初始化以将结构归零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 Win32 API 编程中,通常使用具有多个字段的 C struct.通常只有其中几个具有有意义的值,而所有其他值都必须归零.这可以通过以下两种方式之一实现:

                  In Win32 API programming it's typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways:

                  STRUCT theStruct;
                  memset( &theStruct, 0, sizeof( STRUCT ) );
                  

                  STRUCT theStruct = {};
                  

                  第二个变体看起来更简洁——它是单行的,它没有任何可能输入错误并导致植入错误的参数.

                  The second variant looks cleaner - it's a one-liner, it doesn't have any parameters that could be mistyped and lead to an error being planted.

                  与第一个变体相比,它有什么缺点吗?使用哪种变体以及为什么?

                  Does it have any drawbacks compared to the first variant? Which variant to use and why?

                  推荐答案

                  这两者的含义非常不同.第一个使用 memset 函数,旨在将内存缓冲区设置为特定值.第二个初始化一个对象.让我用一些代码解释一下:

                  Those two constructs a very different in their meaning. The first one uses a memset function, which is intended to set a buffer of memory to certain value. The second to initialize an object. Let me explain it with a bit of code:

                  假设您有一个结构,其成员仅属于 POD 类型(普通旧数据" - 参见 C++ 中的 POD 类型是什么?)

                  Lets assume you have a structure that has members only of POD types ("Plain Old Data" - see What are POD types in C++?)

                  struct POD_OnlyStruct
                  {
                      int a;
                      char b;
                  };
                  
                  POD_OnlyStruct t = {};  // OK
                  
                  POD_OnlyStruct t;
                  memset(&t, 0, sizeof t);  // OK as well
                  

                  在这种情况下写一个 POD_OnlyStruct t = {}POD_OnlyStruct t;memset(&t, 0, sizeof t) 没有太大区别,因为我们这里唯一的区别是 alignment 字节在 memset 使用.由于您通常无法访问这些字节,因此对您来说没有区别.

                  In this case writing a POD_OnlyStruct t = {} or POD_OnlyStruct t; memset(&t, 0, sizeof t) doesn't make much difference, as the only difference we have here is the alignment bytes being set to zero-value in case of memset used. Since you don't have access to those bytes normally, there's no difference for you.

                  另一方面,由于您已将问题标记为 C++,让我们尝试另一个示例,成员类型不同于 POD:

                  On the other hand, since you've tagged your question as C++, let's try another example, with member types different from POD:

                  struct TestStruct
                  {
                      int a;
                      std::string b;
                  };
                  
                  TestStruct t = {};  // OK
                  
                  {
                      TestStruct t1;
                      memset(&t1, 0, sizeof t1);  // ruins member 'b' of our struct
                  }  // Application crashes here
                  

                  在这种情况下,使用像 TestStruct t = {} 这样的表达式是好的,而在其上使用 memset 会导致崩溃.如果你使用 memset 会发生什么 - 创建了一个 TestStruct 类型的对象,从而创建了一个 std::string 类型的对象,因为它是我们结构的成员.接下来,memset 将对象 b 所在的内存设置为某个值,比如零.现在,一旦我们的 TestStruct 对象超出范围,它将被销毁,当轮到它的成员 std::string b 时,您将看到崩溃,因为该对象的所有内部memset 破坏了结构.

                  In this case using an expression like TestStruct t = {} is good, and using a memset on it will lead to crash. Here's what happens if you use memset - an object of type TestStruct is created, thus creating an object of type std::string, since it's a member of our structure. Next, memset sets the memory where the object b was located to certain value, say zero. Now, once our TestStruct object goes out of scope, it is going to be destroyed and when the turn comes to it's member std::string b you'll see a crash, as all of that object's internal structures were ruined by the memset.

                  所以,实际情况是,那些东西非常不同,尽管有时您需要在某些情况下memset将整个结构归零,但始终很重要的是让确保您了解自己在做什么,并且不会像我们的第二个示例那样犯错误.

                  So, the reality is, those things are very different, and although you sometimes need to memset a whole structure to zeroes in certain cases, it's always important to make sure you understand what you're doing, and not make a mistake as in our second example.

                  我的投票 - 对对象使用memset仅在需要时,并使用默认初始化x = {} 在所有其他情况下.

                  My vote - use memset on objects only if it is required, and use the default initialization x = {} in all other cases.

                  这篇关于memset() 或值初始化以将结构归零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么在 MSVC 中默认情况下将浮点值(例如 3.14)视 下一篇:如何在 C++ 中将窗口的屏幕截图作为位图对象获取

                  相关文章

                  最新文章

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

                    <bdo id='4sBXP'></bdo><ul id='4sBXP'></ul>

                  <tfoot id='4sBXP'></tfoot>

                  1. <small id='4sBXP'></small><noframes id='4sBXP'>

                    <legend id='4sBXP'><style id='4sBXP'><dir id='4sBXP'><q id='4sBXP'></q></dir></style></legend>