自定义字符串指针型函数.
定义一函数:
char *a()
{
char *b,*c;
*b="abcdefg";
c=strdup(b);
return(c);
}
返回为c的地址.
以后c是怎么释放内存的?
需要手动free吗?
问题点数:20、回复次数:3Top
1 楼sankt(宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒.)回复于 2006-05-04 19:57:21 得分 20
#include<iostream>
using namespace std;
char * fun()
{
char *b,*c;
b="abcdefg";
c=strdup(b);
return c;
}
int main()
{
char *p=fun();
cout<<p<<endl;
free(p);
p=NULL;
system("pause");
return 0;
}
Top
2 楼sankt(宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒.)回复于 2006-05-04 19:58:12 得分 0
Because _strdup calls malloc to allocate storage space for the copy of strSource, it is good practice always to release this memory by calling the free routine on the pointer returned by the call to _strdup.
Top
3 楼cexoyq(乌鸦)回复于 2006-05-04 20:31:43 得分 0
谢谢.给分结帖.Top




