From: Kevin P. Fleming Date: Thu, 2 Jun 2005 19:17:04 +0000 (+0000) Subject: add a minor allocation/zeroing guideline X-Git-Tag: 1.2.0-beta1~547 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5c33b52cedc714d866a69ee45c587d178543c7db;p=thirdparty%2Fasterisk.git add a minor allocation/zeroing guideline git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5817 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/doc/CODING-GUIDELINES b/doc/CODING-GUIDELINES index 01a38d6f21..c237e92fd4 100755 --- a/doc/CODING-GUIDELINES +++ b/doc/CODING-GUIDELINES @@ -192,6 +192,28 @@ output buffer will be null-terminated. Use ast_copy_string instead, which 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