Do not define large arrays in variable declarations within functions. They will go on the stack and can cause many problems.
Do not explicitly use malloc()
or calloc()
, instead use the AJAX macros e.g. AJALLOC
, AJNEW
, AJCNEW
, AJNEW0
, AJCNEW0
etc (see Section 5.3, “Objects (C Data Structures)”).
Call constructor functions explicitly, i.e. like this:
int main(int argc, char **argv) { AjPStr tmpstr = NULL; tmpstr = ajStrNew(); ajStrAssignC(&tmpstr,"Hello");
not like this where construction occurs (the default string constructor is invoked by ajStrAssignC
) but is not clear from the code:
AjPStr tmpstr = NULL; ajStrAssignC(&tmpstr,"Hello");
If possible call all necessary constructors at the start of the function. They will act as a reminder to put destructor functions at the bottom of the function. Doing this avoids memory leaks.
Your code should not leak memory. Always call the destructor function on any objects as required. This applies within all functions including the main()
one: do not rely on the operating system to free up process memory:
int main(int argc, char **argv) { AjPStr tmpstr = NULL; tmpstr = ajStrNew(); ajStrAssignC(&tmpstr,"Hello"); ajStrDel(&tmpstr); return 0; }
More information on memory management is available (Section 5.3, “Objects (C Data Structures)”).