包裹函数其实是对函数进行封装。将所有可能出现的情况写在一起,以便于程序的调用,提高程序的可读性。
包裹函数和标准函数一样,只是首字母变成大写。
例子:
(1)malloc的包裹函数Malloc
void *Malloc(size_t size)
{
int *p = (int *)malloc(size);
if(p == NULL){
fprintf(stderr, "the memory is full!\n");
exit(1);
}
return p;
}
int main(int atgc, char **argv)
{
char *str = "deftyuhrbvgf";
char *result = (char *)Malloc(strlen(str));
strcpy(result, str);
}
(2)socket()包裹函数Socket()
int Socket(int family, int type, int protocol)
{
int n = socket(family, type, protocol);
if(n < 0){
perror("socket error!\n");
exit(0);
}
return n;
}