What does {0} mean when initializing an object?

后端 未结 10 772
-上瘾入骨i
-上瘾入骨i 2020-11-29 14:46

When {0} is used to initialize an object, what does it mean? I can\'t find any references to {0} anywhere, and because of the curly braces Google s

10条回答
  •  不知归路
    2020-11-29 15:29

    One thing to be aware of is that this technique will not set padding bytes to zero. For example:

    struct foo
    {
        char c;
        int  i;
    };
    
    foo a = {0};
    

    Is not the same as:

    foo a;
    memset(&a,0,sizeof(a));
    

    In the first case, pad bytes between c and i are uninitialized. Why would you care? Well, if you're saving this data to disk or sending it over a network or whatever, you could have a security issue.

提交回复
热议问题