内存耗尽怎么办?

内存耗尽怎么办?

如果在申请动态内存时找不到足够大的内存块,malloc 和 new 将返回 NULL 指针, 宣告内存申请失败。通常有三种方式处理“内存耗尽”问题。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     //声明变量和指针变量
 7     int a,b,c,*ip;
 8 
 9     //指针变量ip指向变量a
10     a=100;
11     ip=&a;        //使指针变量 ip 指向变量a
12     cout<<"a="<<a<<endl;
13     cout<<"*ip="<<*ip<<endl;
14     cout<<"ip="<<ip<<endl;
15 
16     //指针变量ip指向变量b
17     ip=&b;        //使指针变量 ip 指向变量b
18     b=200;
19     cout<<"b="<<b<<endl;
20     cout<<"*ip="<<*ip<<endl;
21     cout<<"ip="<<ip<<endl;
22 
23     //指针变量ip指向变量c
24     ip=&c;        //使指针变量 ip 指向变量b
25     *ip=a+b;
26     cout<<"c="<<c<<endl;
27     cout<<"*ip="<<*ip<<endl;
28     cout<<"ip="<<ip<<endl;
29     return 0;
30 }

 

posted @ 2018-08-02 12:30  borter  阅读(237)  评论(0编辑  收藏  举报