69,369
社区成员
发帖
与我相关
我的任务
分享
void GetMemory(char * p, int num)
{
p = (char *)malloc(num*sizeof(char));
}
int main()
{
char *str = NULL;
GetMemory(str,10);
strcpy(str,"hello");
free(str); //free 并没有起作用,内存泄漏,在GetMemory函数最后加return p 即可
return 0;
}
void GetMemory(char ** p, int num)
{
*p = (char *)malloc(num*sizeof(char));
}
int main()
{
char *str = NULL;
GetMemory( &str,10 );
strcpy(str,"hello");
free(str); //现在free起作用了
return 0;
}