]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
doc: Update codeing guidelines for struct initialisation
authorNoel Power <noel.power@suse.com>
Thu, 27 Jun 2024 08:02:04 +0000 (09:02 +0100)
committerNoel Power <npower@samba.org>
Fri, 28 Jun 2024 10:17:14 +0000 (10:17 +0000)
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Noel Power <npower@samba.org>
Autobuild-Date(master): Fri Jun 28 10:17:14 UTC 2024 on atb-devel-224

README.Coding.md

index 76f2c70e95a263654070b528aa4c9f8260c0c5a7..53a829cb4f293161c9ad0a118895b4f168bc703f 100644 (file)
@@ -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