[Asterisk-cvs] asterisk/doc CODING-GUIDELINES,1.11,1.12

kpfleming at lists.digium.com kpfleming at lists.digium.com
Thu Jun 2 15:14:00 CDT 2005


Update of /usr/cvsroot/asterisk/doc
In directory mongoose.digium.com:/tmp/cvs-serv2755/doc

Modified Files:
	CODING-GUIDELINES 
Log Message:
add a minor allocation/zeroing guideline


Index: CODING-GUIDELINES
===================================================================
RCS file: /usr/cvsroot/asterisk/doc/CODING-GUIDELINES,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- CODING-GUIDELINES	19 May 2005 04:31:02 -0000	1.11
+++ CODING-GUIDELINES	2 Jun 2005 19:17:04 -0000	1.12
@@ -192,6 +192,28 @@
 is also slightly more efficient (and allows passing the actual buffer
 size, which makes the code clearer).
 
+When allocating/zeroing memory for a structure, try to use code like this:
+
+struct foo *tmp;
+
+...
+
+tmp = malloc(sizeof(*tmp));
+if (tmp)
+  memset(tmp, 0, sizeof(*tmp));
+
+This eliminates duplication of the 'struct foo' identifier, which makes the
+code easier to read and also ensures that if it is copy-and-pasted it won't
+require as much editing. In fact, you can even use:
+
+struct foo *tmp;
+
+...
+
+tmp = calloc(1, sizeof(*tmp));
+
+This will allocate and zero the memory in a single operation.
+
 == CLI Commands ==
 
 New CLI commands should be named using the module's name, followed by a verb




More information about the svn-commits mailing list