C ++ - Efficient approach to struct initialization

A colleague and I discussed the effectiveness of some approaches to zeroing a struct

. Suppose we have a simple one struct

like this:

struct {
  int     iMyInt;
  char    cMyChar;
  double  dCoordinates[128];
} s_t;

      

My approach would be to do this:

s_t myStruct = { 0 };

      

Instead, my colleague advised using this approach:

s_t myStruct = { };

      

He noted that my approach initializes one element and then the rest of the elements in a two-part operation, while his approach initializes the whole struct

in a one-part operation, which is faster and more efficient.

Is this true, and if so, is it always at all levels of compiler optimization (i.e. the compiler does and optimizes all of this for me)?

Thank.

+3


source to share


2 answers


First of all, the code you provided is not valid: you must declare this struct type like this

struct s_t {
  int     iMyInt;
  char    cMyChar;
  double  dCoordinates[128];
};

      



That said, in your particular case, the two approaches are the same and everything is zeroed out. In -O3

and even -O0

for clang there is no difference for generic test code that uses any of the above and / or array elements (in -O0

it even calls memset

over the entire structure in both cases).

s_t obj = {0};

movq    %rcx, %rdi
movl    %eax, %esi
callq   memset

s_t obj = {};

movq    %rcx, %rdi
movl    %eax, %esi
callq   memset

      

+6


source


C ++ 11 §8.5.1 / 7:

" If there are fewer initializer clauses in the list than in the aggregate, then each member not explicitly initialized must be initialized from an empty initializer list

And the final touch of C ++ 11 §8.5.4 / 3:

" Otherwise [ie T is not a class, not an aggregate, not a reference], if there are no elements in the initializer list, the object is value-initialized."

A value initialized to a simple type of type int

means null initialization.

Thus, the formal effect {0}

for a type that matters 0

is the same as the formal effect {}

.

However, the standard cannot dictate the quality of compiler execution . The compiler may react to what it thinks are bad words in the filename, and simply because you are calling a lot of dummy code before each function call. Likewise, in theory, you can treat different designations in different ways. generating code even though they mean the same thing.


Repeat your colleague's idea that

" . He noted that my approach initializes one element and then the rest of the elements in a two-part operation, while his approach initializes the entire structure in a one-part operation, which is faster and more efficient.



No, this is just a misconception about how the compiler might work.

Compiled by don & rsquo; t work that way.

But empty curly brace notation has the advantage that it also works, for example, std::string

any other type T

where it T()

is a valid expression but is T(0)

not.

those. it's a little more general.


All this suggests that in C ++ you don't need to do C acrobatics

typedef struct Point_tag
{
    int x, y;
} Point;

      

Instead, you can simply do

struct Point
{
    int x, y;
};

      

+1


source







All Articles