C Hygiene

  • All local scope variable must be initialised.
  • Use calloc instead of malloc since the first one automatically initialises the variable thus following the Rule 1.
  • Avoid taking local variables addresses using & operator as this will inhibit 202205171500# which is great in 202203011139#. See 202205171504.
  • When using pointer as function parameters, use array syntax if you can. The following codes shows how to handle four cases: single object that must not be null, a known size collection of objects, an unknown size collection of objects aka variable length array (VlA), and single object that could be null.
// Case 1
int function(int singleObjectMustNotBeNull[static 1]);

// Case 2, the value could be any num that > 1
int function(int fixedSizeCollection[static 7]);

// Case 3
int function(size_t n, int VLA[n]);

// Case 4, impose checking inside the function body
int function(int *singleObjectCanBeNull);
  • If possible, avoid using goto or jump (setjump and longjump) instructions in order to have an easily understandable control flow.
Links to this page
  • Signal Handling In POSIX

    We could further expand its usage by another two signal jump functions from setjmp.h that can affect the control flow# of the program: sigsetjmp() and siglongjmp().

#c #memory #security #hygiene