leign

Contact: Email: leign.du@gmail.com MSN: dujiali1987@msn.cn
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

  • 随笔 - 12
  • 文章 - 12
  • 评论 - 8
  • 引用 - 0

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

[C++]通过调用函数申请内存空间
函数中处理/通过返回值传递
 1using namespace std;
 2void getMem(char **p,int n);    //通过指针的指针传
 3char *getM(int n);    //通过函数返回值传申请内存空间
 4int main()
 5{
 6    char *p=NULL;
 7    getMem(&p,10);
 8    strcpy(p,"dujiali");
 9    cout<<p<<endl;
10    free(p);
11    char *q=NULL;
12    q=getM(10);
13    strcpy(q,"leign");
14    cout<<q<<endl;
15    free(q);
16    return 0;
17}

18void getMem(char **p,int n)
19{
20    *p=(char *)malloc(sizeof(char)*n);
21}

22char *getM(int n)
23{
24    char *p=(char *)malloc(sizeof(char)*n);
25    return p;
26}

27/* wrong */
28void get(char *p,int n)
29{
30    p=(char *)malloc(sizeof(char)*n);    //函数退出时销毁
31}

posted on 2009-06-04 00:59 leign 阅读(759) 评论(0)  编辑 收藏 引用 所属分类: C++学习


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理