一、申请动态内存函数 int apply_memory(const char **pcDesAddr, const int len):
/*************************************************
Function: int apply_memory(const char **pcDesAddr, const int len)
Description: 申请动态内存
Input: len:申请内存大小
Output: **pcDesAddr:动态内存地址
Return: -1:失败
0:成功
*************************************************/
int apply_memory(const char **pcDesAddr, const unsigned int len)
{
if (!(*pcDesAddr = (char *)malloc(len)))
{
printf("Apply [%d] memory FAIL!\n");
return -1;
}
return 0;
}
输入:
1、const unsigned int len:函数需要申请动态内存大小;
输出:
1、const char **pcDesAddr:动态内存首地址;
char** 是一个指向char*的指针。在函数中如果要对一个参数char *申请空间,同时要在函数外面使用。需要传递char**,否则申请的空间无法在函数外面使用,但申请的空间仍会在,造成空间泄露。