Struct initalization valid ?
-
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
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.
-
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.
Thanks. let's hope 0 or empty strings are not valid values!! :)
CI/CD = Continuous Impediment/Continuous Despair
-
Thanks. let's hope 0 or empty strings are not valid values!! :)
CI/CD = Continuous Impediment/Continuous Despair
-
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
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')
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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