A12-1-1 - Does it apply to POD structs? - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: AUTOSAR C++:2014 rules (https://forum.misra.org.uk/forumdisplay.php?fid=185) +--- Thread: A12-1-1 - Does it apply to POD structs? (/showthread.php?tid=1635) |
A12-1-1 - Does it apply to POD structs? - cgpzs - 23-10-2022 Hi, Does A12-1-1 apply to POD structs? Example: Code: struct Foo The members of `Foo` are by default uninitialized. Does A12-1-1 require `Foo` to explicitly initialize its members, like this? Code: struct Foo Code: { Code: int x{}; Code: int y{}; Code: }; Please note that the above change has some implications, namely that `Foo` is no longer trivial. As such, compilers will warn about performing `memcpy` operations on them. This is a problem for serializer/deserializer type of code. Thanks! RE: A12-1-1 - Does it apply to POD structs? - kent.dorfman766 - 24-10-2022 found this in the parasoft docs...reposting without permission. All member variables should be initialized in constructor [AUTOSAR-A12_1_1-b] DESCRIPTION Constructors of 'class' and 'struct' should initialize all member variables. The rule checks if a member variable is initialized: - in constructor initialization list - inside body of constructor - inside body of function called from constructor (three levels of nested function's calls are checked) - directly in class (C++11). If there is no constructor at all a violation is reported for 'class' only. You can enable INIT-15 to get a violation for 'struct' too. See also: INIT-10, INIT-14, INIT-15, MISRA-030 NOTES The rule assumes that member variable might be initialized by passing its non-const pointer to an external function. Member variables which are static or have class or struct type are ignored. Static variables should not be initialized in constructor. Class/struct variables are assumed to be initialized by their own constructors. If in constructor is called default copy assignment operator, then the rule assumes that all member variables are initialized. If in constructor is called a member function from the current class with non-accessible body, then this rule assumes that all member variables are initialized. BENEFITS Prevents reading from uninitialized variables. RE: A12-1-1 - Does it apply to POD structs? - cgpzs - 25-10-2022 Thanks for the snippet! I experience the same with static analyzers, they split into multiple diagnostics to allow for a finer level of control and specific project deviations. RE: A12-1-1 - Does it apply to POD structs? - misra cpp - 27-10-2023 The intended reading of the rule is ‘if a class has a user-declared constructor, then ….’. As a POD struct cannot have a user-declared constructor (that would stop it being POD), the rule doesn’t apply. RE: A12-1-1 - Does it apply to POD structs? - cgpzs - 30-10-2023 Thank you for the clarification! RE: A12-1-1 - Does it apply to POD structs? - misra cpp - 30-10-2023 Thread closed |