C++ 关于new和delete的一些简单用法

主函数代码
 #include <iostream>     // std::cout
#include <new>          // ::operator new
#include"newdemo.h"
using std::cout;
using std::endl;
struct MyClass {
  int data[2];
  MyClass() {cout << "constructed [" << this << "]\n";}
};

int main () {
    cout<<"new int***********************"<<endl;
    cout<<"0-1:";
    int a(1);
    cout<<a<<endl;
    //delete &a;//error
    cout<<"0-2:"<<endl;
    int *a1=new int(1);
    delete a1;

    cout<<"new struct********************"<<endl;
  cout << "1-0: "<<endl;
  MyClass * p1 = new MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass))
      // and then constructs an object at the newly allocated space

  cout << "1-1: "<<endl;
  MyClass * p2 = new (std::nothrow) MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
      // and then constructs an object at the newly allocated space

  cout << "1-2: "<<endl;
  new (p2) MyClass;
      // does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
      // but constructs an object at p2

  // Notice though that calling this function directly does not construct an object:
  cout << "1-3: "<<endl;
  MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
      // allocates memory by calling: operator new (sizeof(MyClass))
      // but does not call MyClass's constructor

  cout<<"1-4:"<<endl;
  MyClass *p4=new MyClass[3];

  delete p1;
  delete p2;
  delete p3;
  delete p4;
  cout<<endl<<"new class********************* "<<endl;

  cout<<"2-0:"<<endl;
  NewDemo p_0;
  //delete &p_0;//error;

  cout<<"2-1:"<<endl;
   NewDemo *p_1=new NewDemo;
   delete p_1;//删除p1所指空间,造成悬浮指针,p_1仍指向这个空间,空间中存储的东西被擦除,很不安全,所以最好加上p_1=NULL;;
   cout<<p_1<<'\t';//对于此程序来说p_1所指空间是一个新的可分配的空间;
   p_1->print();
   p_1=NULL;

   cout<<"2-2:"<<endl;
   NewDemo *p_2=new(std::nothrow) NewDemo;
   delete p_2;

   cout<<"2-3:"<<endl;
   new NewDemo;
  // delete (new NewDemo);

   cout<<"2-4:"<<endl;
   NewDemo *p_3=(NewDemo*)::operator new(sizeof(NewDemo));
   delete p_3;

   cout<<"new class []***********************"<<endl;

   cout<<"3-1:"<<endl;
   NewDemo *a_1=new NewDemo[3];
   delete[] a_1;

   cout<<"3-2:"<<endl;
   NewDemo a_2[3];
   //delete[] a_2;//Cause an infinite loop
   //delete a-2;//or delete (a_2+1);error;

   cout<<"3-3:"<<endl;
   new NewDemo[3];

   cout<<endl;
  return 0;
}


#ifndef NEWDEMO_H
#define NEWDEMO_H
#include<iostream>
using std::cout;
using std::endl;
class NewDemo
{
public:
    NewDemo(){value=0;cout<<"constructed ["<<this<<"]"<<endl;}
    ~NewDemo(){cout<<"  destruced ["<<this<<"]"<<endl;}
    void print(){cout<<value<<endl;}
private:
    int value;
};

#endif // NEWDEMO_H


输出结果

new int***********************
0-1:1
0-2:
new struct********************
1-0: 
constructed [0x1649010]
1-1: 
constructed [0x1649030]
1-2: 
constructed [0x1649030]
1-3: 
1-4:
constructed [0x1649070]
constructed [0x1649078]
constructed [0x1649080]

new class********************* 
2-0:
constructed [0x7fffcec842b0]
2-1:
constructed [0x1649070]
  destruced [0x1649070]
0x1649070    23367744
2-2:
constructed [0x1649070]
  destruced [0x1649070]
2-3:
constructed [0x1649070]
2-4:
  destruced [0x1649050]
new class []***********************
3-1:
constructed [0x1649058]
constructed [0x164905c]
constructed [0x1649060]
  destruced [0x1649060]
  destruced [0x164905c]
  destruced [0x1649058]
3-2:
constructed [0x7fffcec84300]
constructed [0x7fffcec84304]
constructed [0x7fffcec84308]
3-3:
constructed [0x1649058]
constructed [0x164905c]
constructed [0x1649060]

  destruced [0x7fffcec84308]
  destruced [0x7fffcec84304]
  destruced [0x7fffcec84300]
  destruced [0x7fffcec842b0]
Press <RETURN> to close this window...
中间关于结构体的代码参考www.cplusplus.com/reference/new/operator new/
  


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值