-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(build): replace {0} zero-init with {} #1731
Conversation
Apparently my compiler (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0) does not like initializing structs with {0} at all. Honestly, I'm not convinced this is valid C++ either, though I'm pretty sure it is valid C. E.g. this page: https://en.cppreference.com/w/cpp/language/zero_initialization does not mention {0} as a valid way to zero-initialize a struct. Signed-off-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
@federico-sysdig please take a look |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
LGTM label has been added. Git tree hash: c452a825fecf240f8bed3bc648a14c9a35ecd873
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Great change!
To my surprise, the empty braces syntax does indeed initialize all struct members without a default constructor (such as built-in integers) to zero. Note that this should only apply to C++, not C.
https://en.cppreference.com/w/cpp/language/zero_initialization
I just had to "paranoidly" see it live for myself. For the curious ones, here's the code:
#include <iostream>
#include <cstring>
struct widget
{
int val1;
int val2;
short val3;
int val4;
};
int main()
{
char buffer1[sizeof(widget)];
char buffer2[sizeof(widget)];
memset(buffer1, 1, sizeof(buffer1));
memset(buffer2, 1, sizeof(buffer2));
widget* ptr1 = new (buffer1)widget;
widget* ptr2 = new (buffer2)widget{}; // the magic is here
std::cout << ptr1->val1 << " - "
<< ptr1->val2 << " - "
<< ptr1->val3 << " - "
<< ptr1->val4 << '\n';
std::cout << ptr2->val1 << " - "
<< ptr2->val2 << " - "
<< ptr2->val3 << " - "
<< ptr2->val4 << '\n';
}
Possible output:
16843009 - 16843009 - 257 - 16843009
0 - 0 - 0 - 0
NOTE: I have no idea how your compilation error was never detected. It's another surprise that this code never went through a gcc version similar to yours.
@federico-sysdig: changing LGTM is restricted to collaborators In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Andreagit97, FedeDP, federico-sysdig, gnosek The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Apparently my compiler (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0) does not like initializing structs with {0} at all.
Honestly, I'm not convinced this is valid C++ either, though I'm pretty sure it is valid C. E.g. this page:
https://en.cppreference.com/w/cpp/language/zero_initialization does not mention {0} as a valid way to zero-initialize a struct.
What type of PR is this?
/kind bug
Any specific area of the project related to this PR?
/area build
Does this PR require a change in the driver versions?
What this PR does / why we need it:
Not sure how it works for everybody else, but I'm getting tons of build errors for struct initialization that does seem off.
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: