69,369
社区成员
发帖
与我相关
我的任务
分享
/*子函数体片段*/
return str;
free(str);//释放
str = NULL;//置空
}
int main()/*主函数*/
{
printf("%s", get_num_bit());
system("pause");
return 0;
}
char* get_num_bit()
{
/*子函数体片段*/
static char* buf = (char*)malloc(1024 * 64);
strcpy(buf, "hello world!");
return buf;
}
可以试试这样,函数多次使用,也不会内存浪费了#include<stdio.h>
#include<stdlib.h>
char * get_num_bit()
{
/*子函数体片段*/
char * str = (char*)malloc(sizeof(char)*100);
str = "hello world!";
return str;
}
int main()/*主函数*/
{
char *p = get_num_bit();
printf("%s\n",p);
system("pause");
free(p);
return 0;
}