标签:form class wing SM always IV har ++ efi
1. malloc memory should always use the following form:
int *a = malloc(sizeof(int) * 3); int **b = malloc(sizeof(int*) * 3); int i = 0; for (; i < 3; i++) { b[i] = malloc(sizeof(int) * 3); } char *c = malloc(sizeof(char) * length); char **pc = malloc(sizeof(char *) * 4); for (i = 0; i < 4; i++) { pc[i] = malloc(sizeof(char) * 3); }
2. memset memory to 0 should use the form:
// Using the malloced memory above: memset(a, 0, sizeof(int) * 3); memset(b, 0, sizeof(int*) * 3); for (i = 0; i < 3; i++) { memset(b[i], 0, sizeof(int) * 3); } memset(c, 0, sizeof(char) * 3); memset(pc, 0, sizeof(char *) * 4); for (i = 0; i < 4; i++) { memset(pc[i], 0, sizeof(char) * 3); }
3. malloc(0) might also return address which is non-null. Which is error-prone. Consider use the following malloc. (..my implementation, any suggestion?)
#define SMALLOC(x) \ ({ void *addr; if ((x) == 0) addr = 0; else addr = malloc(x); (addr); })
标签:form class wing SM always IV har ++ efi
原文地址:https://www.cnblogs.com/sansna/p/9124282.html