From: Noel Power Date: Thu, 27 Jun 2024 08:02:04 +0000 (+0100) Subject: doc: Update codeing guidelines for struct initialisation X-Git-Tag: tdb-1.4.11~282 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f824e985167f08df80a5cf635809eed6573b2e13;p=thirdparty%2Fsamba.git doc: Update codeing guidelines for struct initialisation Signed-off-by: Noel Power Reviewed-by: Ralph Boehme Reviewed-by: Douglas Bagnall Reviewed-by: Andreas Schneider Autobuild-User(master): Noel Power Autobuild-Date(master): Fri Jun 28 10:17:14 UTC 2024 on atb-devel-224 --- diff --git a/README.Coding.md b/README.Coding.md index 76f2c70e95a..53a829cb4f2 100644 --- a/README.Coding.md +++ b/README.Coding.md @@ -390,6 +390,48 @@ Bad Example: pointer1 = some_func1(); ``` +### Initialize structs + +All structures MUST be at least initialised to 0/NULL. + +Current recommended initialization: + +```c + struct somestruct { + int ival; + bool bval; + double dval; + char *sval; + }; + + struct somestruct var1 = {}; +``` + +avoid: + +```c + struct somestruct var1 = {0}; +``` + +as it can be less portable, in particular if the first element of the struct in question is a nested struct. + +Of course if specific members need non-zero initialization then use something like: + +```c + struct bar { + int inner; + }; + struct foo { + int outer; + struct bar nested; + }; + struct foo var2 = { + .outer = 5, + .nested = { + .inner = 3, + }, + }; +``` ### Make use of helper variables