-3

If I have the following struct:

struct MyStruct {
    int *a;
    int *b;
};

And initializes it like this:

int some_var;
MyStruct s{
    .a = &some_var
};

can I be sure s.b will be initialized to nullptr?

EDIT:

Full compiling code, tested with g++ 7.3.0:

// test.cpp
struct MyStruct {
    int *a;
    int *b;
};

int main()
{
    int some_var;
    MyStruct s{
        .a = &some_var
    };
}

If I print the variable values at this sample, b is indeed 0. But I want to know if this behavior is guaranteed by the standard.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
lvella
  • 12,754
  • 11
  • 54
  • 106
  • That doesn't look like `C++`. – Galik Jul 08 '18 at 20:04
  • You can be sure it's a syntax error. –  Jul 08 '18 at 20:04
  • 2
    g++ prints this error message: `warning: C++ designated initializers only available with -std=c++2a or -std=gnu++2a` anybody knows whether this is planned and where I could read the draft/proposal? E: Found something: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0329r0.pdf – tkausl Jul 08 '18 at 20:06
  • @NeilButterworth Compiles with g++ 7.3, it is standard C++11. – lvella Jul 08 '18 at 20:08
  • 5
    It is _not_ standard C++11. The fact that GCC compiles it proves nothing - GCC compiles all sorts of non-standard stuff. Use the `-pedantic` option to filter some of it out. –  Jul 08 '18 at 20:10
  • 2
    "Is struct member pointers automatically initialized to zero?" - No. – Jesper Juhl Jul 08 '18 at 20:10
  • `s.b` will not be initialized. If it was a global variable, it would be zero-initialized. Some compilers zero-initialize variables when compiling with debug options, though. – Sid S Jul 08 '18 at 20:11
  • @NeilButterworth It doesn't mean it isn't, either. But I am pretty sure it is standard. It also compiles with clang with `-std=c++11`. – lvella Jul 08 '18 at 20:13
  • 2
    It is NOT part of Standard C++ 11 (or indeed of any of the later standards up to this date). –  Jul 08 '18 at 20:14
  • Why not just initialize them to `nullptr` explicitly? Like this: `int *a = nullptr;` – L. Kue Jul 08 '18 at 20:14
  • 1
    C++ guarantees you nothing in this case. The implementation may zero the stack, assuming it's using a stack, at start-up and you're taking advantage of that to get null pointers in your sample code. You cannot count on this behaviour. By the way, " warning: ISO C++ does not allow C99 designated initializers [-Wpedantic]". My understanding (thank you @NeilButterworth ) is that support may be added in C++ 20. – user4581301 Jul 08 '18 at 20:14
  • Some helpful reading: https://en.cppreference.com/w/cpp/language/default_initialization – user4581301 Jul 08 '18 at 20:20
  • And probably duplicate: [Variable initialization in C++](https://stackoverflow.com/questions/2218254/variable-initialization-in-c) – user4581301 Jul 08 '18 at 20:21
  • @user4581301 it kinda very special case, and documentation on GCC should be referred instead of C++ standard.. because this is a GCC feature. I vote to remove c++11 tag. – Swift - Friday Pie Jul 09 '18 at 12:54

1 Answers1

1

Note, this is C99 syntax, which is not supported by C++11 standard, but supported by GCC. GCC documentation states so that that omitted field members are implicitly initialized the same as objects that have static storage duration.

Alternate syntax would be

MyStruct s{
        a: &some_var
    };

If the same field is initialized multiple times, it would be initialized with the value from the last initialization. If such initialization causes side-effect, it is unspecified whether the side effect happens or not. GCC discards them, only last initialization happens.

Here is C++11 compatible initialization:

MyStruct s { &some_var };  // second field would be initialized as one with static duration

GCC would issue a warning about missing initializers in both cases.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42