1、内存操作
(1)释放内存操作
释放内存并将指针置空
#define FREE(ptr) if(NULL != ptr) \
free(ptr); \
ptr = NULL;
(2)内存操作
memcpy
memset
memmove
malloc
calloc
memcmp
2、c语言整型数和字符串的转换
(1)字符串到整型数
int atoi(const char *nptr);
(2)整型数到字符串,可用sprintf格式化输出
int sprintf(char *str, char * format [, argument, ...]);
3、单字节对齐
在进程间通信使用单字节对齐,避免一些问题。
#define PACKED __attribute__((__packed__))//单字节对齐
#pragma pack(push) //保存对齐状态
#pragma pack(4)//设定为4字节对齐
struct test
{
char m1;
double m4;
int m3;
};
...(code)...
#pragma pack(pop)//恢复对齐状态
4、计算数组的大小
#define ARRSIZE(array_name) (sizeof(array_name)/sizeof(array_name[0]))
5、IP兼容设计,存储为字符串
typedef enum tagIPTYPE
{
IP_NONE = 0,
IP_v4,
IP_v6,
IP_v4v6
}IP_TYPE_E;
//netinet/in.h
#define IPv4_ADDRSTRLEN 16
#define IPv6_ADDRSTRLEN 46
typedef struct tagIPADDR
{
IP_TYPE_E enIPType;
char acIPv4[IPv4_ADDRSTRLEN];
char acIPv6[IPv6_ADDRSTRLEN];
}PACKED IP_ADDR_S;
6、整型数范围
8位char类型:-2^7 ~ 2^7-1
8位unsigned char:即uint8,0~2^8-1
32位int类型:-2^31 ~ 2^31-1,即-2147483648~2147483647
32位unsigned int:即uint32,0~2^32-1,值4294967295
7、log等级一般设定:note/warning/error/brief/detail
8、回调
(1)同步调用;阻塞方式,单向调用
(2)回调,双向
(3)异步调用,即通过异步消息进行通知
9、字符串处理
(1)字符 ' ';字符串" ";
(2)字符串处理函数
char* strstr(const char* haystack, const char* needle);
char* strchr(const char* s, int c);
char* strtok(char* str,const char* delim);
10、入参检查、返回值检查、变量使用前必须考虑是否需要清空
11、C语言中.cpp文件的使用
#ifdef __cplusplus
extern "c" {
#endif
......
......
#ifdef __cplusplus
}
#endif