Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Struct initalization valid ?

Struct initalization valid ?

Scheduled Pinned Locked Moved C / C++ / MFC
devopsquestion
13 Posts 7 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Maximilien

    I'm looking at old code. I don't think it's something I ever done. It seems weird that the compiler accept to not fully initialize the struct. They (old retired programmers) seem to expect the values to be zero-initialized. I find it not really safe. I caught that with the clang/tidy checker.

    #define sz_Potato "Patate"
    struct MyStruct
    {
    int a;
    int b;
    const char* text;
    short c;
    };

    MyStruct myStructArray[3] =
    {
    {1, 2},
    {1, 2, sz_Potato},
    {1, 2, sz_Potato, 4}
    };

    CI/CD = Continuous Impediment/Continuous Despair

    M Offline
    M Offline
    markkuk
    wrote on last edited by
    #4

    Maximilien wrote:

    It seems weird that the compiler accept to not fully initialize the struct. They (old retired programmers) seem to expect the values to be zero-initialized.

    According to cppreference.com:[^]

    Quote:

    All members that are not initialized explicitly are empty-initialized.

    The old programmers are correct.

    M 1 Reply Last reply
    0
    • M markkuk

      Maximilien wrote:

      It seems weird that the compiler accept to not fully initialize the struct. They (old retired programmers) seem to expect the values to be zero-initialized.

      According to cppreference.com:[^]

      Quote:

      All members that are not initialized explicitly are empty-initialized.

      The old programmers are correct.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #5

      Thanks. let's hope 0 or empty strings are not valid values!! :)

      CI/CD = Continuous Impediment/Continuous Despair

      C 1 Reply Last reply
      0
      • M Maximilien

        Thanks. let's hope 0 or empty strings are not valid values!! :)

        CI/CD = Continuous Impediment/Continuous Despair

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #6

        Well, 0 is the best bet. :)

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        1 Reply Last reply
        0
        • M Maximilien

          I'm looking at old code. I don't think it's something I ever done. It seems weird that the compiler accept to not fully initialize the struct. They (old retired programmers) seem to expect the values to be zero-initialized. I find it not really safe. I caught that with the clang/tidy checker.

          #define sz_Potato "Patate"
          struct MyStruct
          {
          int a;
          int b;
          const char* text;
          short c;
          };

          MyStruct myStructArray[3] =
          {
          {1, 2},
          {1, 2, sz_Potato},
          {1, 2, sz_Potato, 4}
          };

          CI/CD = Continuous Impediment/Continuous Despair

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #7

          Maximilien wrote:

          I'm looking at old code.

          You would need to make that more specific. And provide more context. Since C didn't initialize them and C++ was initially based on that if it was, for example, code from the 80s then perhaps. But I don't think back then they had array structure initializers.

          Maximilien wrote:

          seem to expect the values to be zero-initialized.

          From "The C++ Programing Language Special Edition" book by Stroustrup with a 2nd Printing of March 2000. I believe that book was based on the first ANSI C++ standard. Section "4.9.5 Initialization" "If no initializer is specified ...is initialized to 0 of appropriate type" "Members of arrays and structures are default initialized or not depending on whether the array or structure is static" Myself looking at that code, with no other context provided, yes it should be initialized to zero ('text' and 'c')

          1 Reply Last reply
          0
          • M Maximilien

            I'm looking at old code. I don't think it's something I ever done. It seems weird that the compiler accept to not fully initialize the struct. They (old retired programmers) seem to expect the values to be zero-initialized. I find it not really safe. I caught that with the clang/tidy checker.

            #define sz_Potato "Patate"
            struct MyStruct
            {
            int a;
            int b;
            const char* text;
            short c;
            };

            MyStruct myStructArray[3] =
            {
            {1, 2},
            {1, 2, sz_Potato},
            {1, 2, sz_Potato, 4}
            };

            CI/CD = Continuous Impediment/Continuous Despair

            H Offline
            H Offline
            honey the codewitch
            wrote on last edited by
            #8

            I typically use {0} to initialize a struct with a number of fields. It works. I imagine it can be extended to do the above as you provided, but probably because it fills in remainders with zeroes. I don't know how unsafe it is. I can tell you it works with GCC, Clang, and MSVC (at least as of 2022) in my experience.

            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

            K 1 Reply Last reply
            0
            • H honey the codewitch

              I typically use {0} to initialize a struct with a number of fields. It works. I imagine it can be extended to do the above as you provided, but probably because it fills in remainders with zeroes. I don't know how unsafe it is. I can tell you it works with GCC, Clang, and MSVC (at least as of 2022) in my experience.

              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #9

              For C code, using {0} is safe, as far as I know, and initializes all bytes of the struct to zero. For C++, I don't know. It looks like maybe all bytes are initialized to zero, as per C, but then constructors are called with an empty argument list. If the first struct member has a constructor, then the compiler will look for a constructor that takes a single int. If no such constructor exists, compilation fails. Both g++ and clang++ warn about struct members with missing initial values when -Wextra is used. For C, though, it can be useful, since you can also use named initializers e.g.

              struct S {
              int i;
              double d;
              char str[10];
              };

              struct S s { .d = 123.4; };

              will assign 123.4 to s.d, and all other members will be set to zero. You can use named initializers in C++ too, but you still get warnings about struct members with missing initializers.

              Keep Calm and Carry On

              H 1 Reply Last reply
              0
              • K k5054

                For C code, using {0} is safe, as far as I know, and initializes all bytes of the struct to zero. For C++, I don't know. It looks like maybe all bytes are initialized to zero, as per C, but then constructors are called with an empty argument list. If the first struct member has a constructor, then the compiler will look for a constructor that takes a single int. If no such constructor exists, compilation fails. Both g++ and clang++ warn about struct members with missing initial values when -Wextra is used. For C, though, it can be useful, since you can also use named initializers e.g.

                struct S {
                int i;
                double d;
                char str[10];
                };

                struct S s { .d = 123.4; };

                will assign 123.4 to s.d, and all other members will be set to zero. You can use named initializers in C++ too, but you still get warnings about struct members with missing initializers.

                Keep Calm and Carry On

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #10

                Wonder why I don't get any warnings doing that under zephyr, which is pretty darn strict in terms of how it configures GCC. I usually use it to test warnings. I think it's set to -Wall It's possible I'm not using the {0} technique in any of my major libs, but I could swear I am.

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                K 1 Reply Last reply
                0
                • H honey the codewitch

                  Wonder why I don't get any warnings doing that under zephyr, which is pretty darn strict in terms of how it configures GCC. I usually use it to test warnings. I think it's set to -Wall It's possible I'm not using the {0} technique in any of my major libs, but I could swear I am.

                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #11

                  You do need to use -Wextra to get those warnings:

                  [k5054@localhost]$ cat example.cpp
                  #include struct S
                  {
                  int i;
                  float f;
                  double d;
                  };

                  int main()
                  {
                  S s{0};

                  std::cout << s.f << '\\n';
                  

                  }
                  [k5054@localhost]$ g++ example.cpp
                  [k5054@localhost]$ g++ example.cpp -Wall
                  [k5054@localhost]$ g++ example.cpp -Wextra
                  example.cpp: In function ‘int main()’:
                  example.cpp:12:11: warning: missing initializer for member ‘S::f’ [-Wmissing-field-initializers]
                  12 | S s{0};
                  | ^
                  example.cpp:12:11: warning: missing initializer for member ‘S::d’ [-Wmissing-field-initializers]
                  [k5054@localhost]$

                  Keep Calm and Carry On

                  H 1 Reply Last reply
                  0
                  • K k5054

                    You do need to use -Wextra to get those warnings:

                    [k5054@localhost]$ cat example.cpp
                    #include struct S
                    {
                    int i;
                    float f;
                    double d;
                    };

                    int main()
                    {
                    S s{0};

                    std::cout << s.f << '\\n';
                    

                    }
                    [k5054@localhost]$ g++ example.cpp
                    [k5054@localhost]$ g++ example.cpp -Wall
                    [k5054@localhost]$ g++ example.cpp -Wextra
                    example.cpp: In function ‘int main()’:
                    example.cpp:12:11: warning: missing initializer for member ‘S::f’ [-Wmissing-field-initializers]
                    12 | S s{0};
                    | ^
                    example.cpp:12:11: warning: missing initializer for member ‘S::d’ [-Wmissing-field-initializers]
                    [k5054@localhost]$

                    Keep Calm and Carry On

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #12

                    Thank you. I think I'll add that to my build environment.

                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                    K 1 Reply Last reply
                    0
                    • H honey the codewitch

                      Thank you. I think I'll add that to my build environment.

                      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #13

                      Take a look at the GCC (or clang) documentation for what -Wextra adds: [Warning Options (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#:~:text=--,Wextra,-¶) . In most cases, you probably do want to be aware of all the additional warnings that -Wextra brings. But maybe in the embedded world you're going to be doing something that's going to trigger one of the warnings excessively. In which case you might just want -Wmissing-field-initializers. Or perhaps you might want e.g. -Wextra -Wno-unintialized, which gives you all of -Wextra's goodness, but silences the -Wuninitailzed warnings. If you've got a section of code that you know is going to generate warnings, and you just want to shut the compiler up you can always investigate the GCC diagnostic pragmas : [Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html)

                      Keep Calm and Carry On

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups