[初]对于C++内存模型的一个测试小程序

#include <iostream>
#include <cstdio>

using namespace std;

//对于C++内存模型的一个测试小程序
//author:snail
//date:2007-03-02


//*

class MemoryKiller{
public:
    MemoryKiller(){};
private:
    int bigMemory[1024*1024]; 
};

//全局区(静态区)(static):全局变量和静态变量的存储是放在一块的
//下面这行需要800的内存,而且没有出错,说明gcc编译器应该没有对静态区
//做大小限制~~任意大小:)
int Global_Memory_killer[200000000];

int main(){
    // 在这里,我又向系统要了800M内存!
    int* p = new int[200000000];
   
    //堆区(heap):gcc编译器对堆区的大小也没有做任何限制。
    MemoryKiller *mk = new MemoryKiller();
    cout << "create MemoryKiller on the heap successful!" << endl;

    //MemoryKiller k是这样建立在栈上还是建立在堆上?
    //这个问题困扰了我好久,结果程序跑起来,结果马上就出现了(挂了。。)
    //最终还是验证对象是建立在堆栈上的~~~
    //MemoryKiller k;
   
    cout << "create MemoryKiller on the stack successful!" << endl;
   
    //在这里我又向系统要800内存,不过这次可能就没这么幸运了!
    //这时候系统对我说这么多了,不过系统有2种表达方式,一种
    //丢出异常,另一种丢个0给我,因为我在申请空间的时候明确
    //告诉系统不要丢异常,就是(nothrow)这个东西,明确说明我要0.
    //所以系统返回个0给我,真听话啊!
    if(int *q = new(nothrow) int[200000000]){
        cout << "success!";
    }else{
        cout << "lost!";
    }  
   
    system("pause");
}

//*/
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值