【郝斌数据结构自学笔记】10-11_跨函数使用内存讲解及其示例

10_跨函数使用内存讲解及其示例

CASE 1

int f();

int main(void)

{

         inti=10;

         i=f();

         printf(“i=%d\n”,i);

         for(i=0;i<2000;++i)

                   f();

 

         return0;

}

int f()

{

         intj=20;

         returnj;

}

 

CASE 2

main ()

{

         int*p;

fun(&p);

...

}

int fun (int **q)

{

         ints;        //s为局部变量。调用完毕后s就没有了,最终p没有指向一个合法的整型单元

         *q=&s;

}

 

CASE 3

main()

{

         int*p;

         fun(&p);

         ...

}

int fun(int **q)

{

         *q=(int*)malloc(4); //返回4个字节,只取第1个字节地址赋给*q,*q==p。执行完后,因为没有free(),内存没有释放。如果没有free(),整个程序彻底终止时才能释放

}

 

程序内部类定义方法

A aa=new A();

A *pa=(A*)malloc(sizeof(A));

 

CASE 4

#include<stdio.h>

#include<malloc.h>

struct Student

{

         intsid;

         intage;

}

 

struct Student *  CreatStudent(void);

void ShowStudent(struct Student *);

int main(void)

{

         structStudent *ps;

         ps=CreatStudent();

         ShowStudent(ps);

 

         return0;

}

struct Student *  CreatStudent(void)

{

         structStudent *P=(struct Student *)malloc(sizeof(struct Student));

         p->sid=99;

         p->age=88;

return p;

}

void ShowStudent(struct Student *pst)

{

         printf(“%d%d\n”,pst->sid,pst->age);

}

11_复习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值