Initializing an object to all zeroes

后端 未结 12 587
离开以前
离开以前 2020-12-03 01:27

Oftentimes data structures\' valid initialization is to set all members to zero. Even when programming in C++, one may need to interface with an external API for which this

相关标签:
12条回答
  • 2020-12-03 02:16

    In C I prefer using {0,} to the equivalent memset(). However gcc warns about this usage :( Details here: http://www.pixelbeat.org/programming/gcc/auto_init.html

    In C++ they're usually equivalent, but as always with C++ there are corner cases to consider (noted in other answers).

    0 讨论(0)
  • 2020-12-03 02:17

    If the struct contains pointers, the value of all bits zero as produced by memset may not mean the same as assigning a 0 to it in the C (or C++) code, i.e. a NULL pointer.

    (It might also be the case with floats and doubles, but that I've never encountered. However, I don't think the standards guarantee them to become zero with memset either.)

    Edit: From a more pragmatic perspective, I'd still say to not use memset when possible to avoid, as it is an additional function call, longer to write, and (in my opinion) less clear in intent than = { 0 }.

    0 讨论(0)
  • 2020-12-03 02:19

    I think the initialization speaks much clearer what you actually are doing. You are initializing the struct. When the new standard is out that way of initializing will get even more used (initializing containers with {} is something to look forward to). The memset way are slightly more error prone, and does not communicate that clearly what you are doing. That might not account for much while programming alone, but means a great deal when working in a team.

    For some people working with c++, memset, malloc & co. are quite esoteric creatures. I have encountered a few myself.

    0 讨论(0)
  • 2020-12-03 02:20

    Hopefully it is understood that this is only currently available for POD structures; you'd get a compiler error if there was a C++ std::string in that structure.

    No you won't. If you use memset on such, at the best you will just crash, and at the worst you get some gibberish. The = { } way can be used perfectly fine on non-POD structs, as long as they are aggregates. The = { } way is the best way to take in C++. Please note that there is no reason in C++ to put that 0 in it, nor is it recommended, since it drastically reduces the cases in which it can be used

    struct A {
      std::string a;
      int b;
    };
    
    int main() {
      A a = { 0 };
      A a = { };
    }
    

    The first will not do what you want: It will try to create a std::string from a C-string given a null pointer to its constructor. The second, however, does what you want: It creates an empty string.

    0 讨论(0)
  • 2020-12-03 02:24

    I've never understood the mysterious goodness of setting everything to zero, which even if it is defined seems unlikely to be desirable. As this is tagged as C++, the correct solution to initialisation is to give the struct or class a construtor.

    0 讨论(0)
  • 2020-12-03 02:25

    I found a good solution to be:

    template<typename T> void my_zero(T& e) {
        static T dummy_zero_object;
        e = dummy_zero_object;
    }
    
    my_zero(s);
    

    This does the right thing not only for fundamental types and user-defined types, but it also zero-initializes types for which the default constructor is defined but does not initialize all member variables --- especially classes containing non-trivial union members.

    0 讨论(0)
提交回复
热议问题