-
-
Notifications
You must be signed in to change notification settings - Fork 678
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
STYLE: Add in-class {}
member initializers to objects created by New()
#3851
STYLE: Add in-class {}
member initializers to objects created by New()
#3851
Conversation
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.
Wouldn't this make it harder to spot a failure to really initialize these ivars in the constructor, or elsewhere?
Good question, but I'm not sure... Suppose a numeric data member is just zero-initialized by The use of uninitialized data isn't always spotted automatically, it may just have undefined behavior silently. |
Instead of a bulk commit like this, we could just keep chasing uninitialized member variables one by one, as in PR #3845 (by Jon) #3843, and #3849 (by myself). Which is certainly useful, but also very time consuming. And we probably won't fix all of them that way, one by one. I think it's preferable to just initialize all data members by default. And only leave some particular data uninitialized in very special (performance critical) cases. What do you think? |
I have mildly positive attitude towards this PR. I would like some other people to pitch in. |
ITK.Linux has some warning at https://open.cdash.org/viewBuildError.php?type=1&buildid=8377051, but it seems unrelated to this PR, I don't know:
|
FYI Here's a cheat sheet for the value initialized by
|
/azp run ITK.Linux |
2bc139a
to
e5e27a9
Compare
For the record, here is the discussion topic that I started on this subject: Adding in-class The script I wrote to do the code changes is at https://gist.github.com/N-Dekker/738d28b8d7528a32c12e7a129160b00f |
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.
👏 ✨
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.
Thanks for having taken the time to do this Niels.
A couple of in-line comments.
Please:
- Translate the gist (:ok_hand:) into a script file so that it dwells in the ITK Maintenance Utilities. Remotes may be grateful at some point.
- Apply this to non-default modules and examples (if not already done).
- Write this in the ITK SW guide.
Give a few more days for others to comment/review, then merge? But corresponding update should go into the software guide either concurrently with the merge of this, or very soon afterwards. |
Thanks for your feedback @dzenanz
Sure, that's fine to me!
Actually, it already says:
I just made a pull request to no longer exclude smart pointers. Instead, it proposes to exclude low level utility classes. Please check: InsightSoftwareConsortium/ITKSoftwareGuide#192 |
e5e27a9
to
27d1af3
Compare
For an object created by `New()`, the performance cost of adding a `{}` default member initializer to each of its non-static data members should be neglectable compared to the cost of allocating the object itself, as it is allocated on the heap. In-class default member initialization effectively prevents Valgrind/Memcheck warnings like "Conditional jump or move depends on uninitialised value(s)", and Visual C++ Code Analysis warnings like "warning C26495: Variable is uninitialized. Always initialize a member variable (type.6)" These cases are found using the regular expression ` \w.* m_\w[^{}=]+;`, excluding `static` data members and data members of a reference (`&`) type. `Octree::m_ColorTable` was excluded, to avoid GCC warnings saying "const/copy propagation disabled" and "GCSE disabled" [-Wdisabled-optimization].
27d1af3
to
a7aae35
Compare
For your information, the last force-push just did another |
Added in-class `{}` default member initializers to data members of classes that have a `itkSimpleNewMacro` macro call. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()" (which only looked at classes that have a `itkNewMacro`).
Added in-class `{}` default member initializers to data members of classes that have one or more virtual member functions. For an object that has one or more virtual member functions, the performance cost of adding a `{}` default member initializer to each of its non-static data members should be acceptable, relative to cost of the initialization of its virtual table. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()" (which only looked at classes that have a `itkNewMacro`).
Added in-class `{}` default member initializers to data members of classes that have one or more virtual member functions. For an object that has one or more virtual member functions, the performance cost of adding a `{}` default member initializer to each of its non-static data members should be acceptable, relative to cost of the initialization of its virtual table. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()" (which only looked at classes that have a `itkNewMacro`).
Added in-class `{}` default member initializers to data members of classes that have one or more virtual member functions. For an object that has one or more virtual member functions, the performance cost of adding a `{}` default member initializer to each of its non-static data members should be acceptable, relative to cost of the initialization of its virtual table. Follow-up to pull request #3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()" (which only looked at classes that have a `itkNewMacro`).
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default"
Previous commits that added `{}` initializers to data members by means of regular expressions had overlooked three-letter identifiers, like `m_E` and `m_I`. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default" pull request InsightSoftwareConsortium#3941 "STYLE: Add in-class `{}` member initializers to 3-letter data members"
Previous commits that added `{}` initializers to data members by means of regular expressions had overlooked member declarations that had a trailing comment. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()"
Previous commits that added `{}` initializers to data members by means of regular expressions had overlooked member declarations that had a trailing comment. Follow-up to pull request InsightSoftwareConsortium#3851 commit 5e2c49f "STYLE: Add in-class `{}` member initializers to objects created by New()"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default" pull request InsightSoftwareConsortium#3941 "STYLE: Add in-class `{}` member initializers to 3-letter data members" pull request InsightSoftwareConsortium#3945 "STYLE: Add in-class `{}` member initializers having trailing comments"
The script that was used for the following pull requests: pull request InsightSoftwareConsortium#3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request InsightSoftwareConsortium#3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request InsightSoftwareConsortium#3917 "STYLE: Add in-class `{}` initializers to classes with override = default" pull request InsightSoftwareConsortium#3941 "STYLE: Add in-class `{}` member initializers to 3-letter data members" pull request InsightSoftwareConsortium#3945 "STYLE: Add in-class `{}` member initializers having trailing comments"
The script that was used for the following pull requests: pull request #3851 "STYLE: Add in-class `{}` member initializers to objects created by New()" pull request #3913 "STYLE: Add in-class `{}` initializers to classes with virtual functions" pull request #3917 "STYLE: Add in-class `{}` initializers to classes with override = default" pull request #3941 "STYLE: Add in-class `{}` member initializers to 3-letter data members" pull request #3945 "STYLE: Add in-class `{}` member initializers having trailing comments"
For an object created by
New()
, the performance cost of adding a{}
default member initializer to each of its non-static data members should be neglectable compared to the cost of allocating the object itself, as it is allocated on the heap.In-class default member initialization effectively prevents Valgrind/Memcheck warnings like "Conditional jump or move depends on uninitialised value(s)", and Visual C++ Code Analysis warnings like "warning C26495: Variable is uninitialized. Always initialize a member variable (type.6)"
These cases are found using the regular expression
\w.* m_\w[^{}=]+;
, excludingstatic
data members and data members of a reference (&
) type.