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

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

      2. <tfoot id='6a2Wr'></tfoot>
          <bdo id='6a2Wr'></bdo><ul id='6a2Wr'></ul>
      3. memset() 或值初始化以将结构归零?

        memset() or value initialization to zero out a struct?(memset() 或值初始化以将结构归零?)

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

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

              <tbody id='aS3Ew'></tbody>

            <tfoot id='aS3Ew'></tfoot>

                <bdo id='aS3Ew'></bdo><ul id='aS3Ew'></ul>
                  本文介绍了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() 或值初始化以将结构归零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  <legend id='AlZh1'><style id='AlZh1'><dir id='AlZh1'><q id='AlZh1'></q></dir></style></legend>
                  • <small id='AlZh1'></small><noframes id='AlZh1'>

                        <tbody id='AlZh1'></tbody>
                      <tfoot id='AlZh1'></tfoot>
                          <bdo id='AlZh1'></bdo><ul id='AlZh1'></ul>

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