Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273127
  • 博文数量: 55
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 737
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-13 18:06
文章分类

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类: C/C++

2008-08-22 02:03:10

#include <iostream>
#include <exception>
using namespace std;

int main () throw ( exception )
{
    const unsigned long SIZE = 500000000;
    char *ptr[10];

    try
    {
        for ( int counter = 0; counter < 10; counter++ )
        {
            ptr[counter] = new char[SIZE];
            cout << "Memory for ptr[" << counter
                 << "] is allocated" << endl;
        }
    }
    catch (exception &e)
    // catch ( ... )

    {
        cout << "Exception: ";
        cout << e.what() << endl;
        return 1;
    }
    
    for ( int i = 0; i < 10; i++ )
        delete [] ptr[i];

    cout << "End" << endl;
    return 0;
}
OUTPUT:
// Memory for ptr[0] is allocated

// Memory for ptr[1] is allocated

// Memory for ptr[2] is allocated

// Memory for ptr[3] is allocated

// Memory for ptr[4] is allocated

// Exception: bad_alloc

阅读(1163) | 评论(0) | 转发(0) |
0

上一篇:Qt的绘图系统

下一篇:一些好玩的字

给主人留下些什么吧!~~