5.new和delete

C++内存管理与大小
本文通过C++代码示例介绍了如何使用new和delete进行内存分配与释放,并展示了不同数据类型所占内存大小及指针变量的大小。
 #include <iostream>
 int main(void){
    using namespace std;
    int night = 1001;
    int * pt = new int;
    *pt = 1001;
    cout<<"night value = "<<night<<" ; location "<<&night<<endl;
    cout<<"int "<<"value = "<<*pt<<" ; location "<<pt<<endl;
    double *pd = new double;
    *pd = 10001.0;
    cout<<"double value = "<<*pd<<" ; location "<<pd<<endl;
    cout<<"size of double = "<<sizeof(double)<<"; size of pd : "<<sizeof(pd)<<" size of *pf : "<<sizeof(*pd)<<endl;
    cout<<"size of int = "<<sizeof(int)<<"; size of pt : "<<sizeof(pt)<<"size of *pt : "<<sizeof(*pt)<<endl;
    delete pd;
    cout<<"delete pd"<<" pd is :"<<pd<<endl;
    delete pt;
    cout<<"delete pt"<<" pt is :"<<pt;
     return 0;
 }

new和delete相当于malloc和free

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值