Skip to content
Snippets Groups Projects
Commit 01a2a2a1 authored by Anthony Minessale II's avatar Anthony Minessale II
Browse files

Modify code example suggestion

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4844 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent 581ae735
Branches
Tags
No related merge requests found
...@@ -124,21 +124,32 @@ the scope of your function try ast_strdupa() or declare struts static ...@@ -124,21 +124,32 @@ the scope of your function try ast_strdupa() or declare struts static
and pass them as a pointer with &. and pass them as a pointer with &.
If you are going to reuse a computable value, save it in a variable If you are going to reuse a computable value, save it in a variable
instead of recomputing it over and over. instead of recomputing it over and over. This can prevent you from
making a mistake in subsequent computations, make it easier to correct
if the formula has an error and may or may not help optimization but
will at least help readability.
Just an Example: Just an example, so don't over analyze it, that'd be a shame:
const char *prefix = "pre";
const char *postfix = "post";
char *newname = NULL;
char *name = "data";
if (name && (newname = (char *) alloca(strlen(name) + strlen(prefix) + strlen(postfix) + 3)))
snprintf(newname, strlen(name) + strlen(prefix) + strlen(postfix) + 3, "%s/%s/%s", prefix, name, postfix);
if (strlen(name)) {
newname = alloca(strlen(name));
strncpy(newname, name, strlen(name);
}
vs vs
if((len = strlen(name))) { const char *prefix = "pre";
newname = alloca(len); const char *postfix = "post";
strncpy(newname, name, len); char *newname = NULL;
} char *name = "data";
int len = 0;
if (name && (len = strlen(name) + strlen(prefix) + strlen(postfix) + 3) && (newname = (char *) alloca(len)))
snprintf(newname, len, "%s/%s/%s", prefix, name, postfix);
Use const on pointers which your function will not be modifying, as this Use const on pointers which your function will not be modifying, as this
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment