TOC | Prev | Next

Macro safety

Always wrap your macro arguments in parens, because of order of operations

#define BYTES_NEEDED(n,type) n * sizeof(type)
const int bytes = BYTES_NEEDED(n+1,int)

becomes

#define BYTES_NEEDED(n,type) n * sizeof(type)
const int bytes = nusers+1 * sizeof(int); /* nusers+1 * sizeof(int); */

which is nusers + (1 * sizeof(int)), not the same.

Instead, define it as:

#define BYTES_NEEDED(n,type) ((n)*sizeof(type))

so it expands as

int bytes = ((nusers+1) * sizeof(int));
TOC | Prev | Next