一般在使用new  和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数

但是也有办法将这两个过程分开

那就是显式的调用::operator new, ::operator delete,它们只进行空间配置,并不调用对象的析构函数

具体的可以参看下面这个例子:

// operator new[] example
#include <iostream> // std::cout
#include <new> // ::operator new[] struct MyClass {
int data;
MyClass() {std::cout << '*';} // print an asterisk for each construction
}; int main () {
std::cout << "constructions (1): ";
// allocates and constructs five objects:
MyClass * p1 = new MyClass[5];
std::cout << '\n'; std::cout << "constructions (2): ";
// allocates and constructs five objects (nothrow):
MyClass * p2 = new (std::nothrow) MyClass[5];
std::cout << '\n'; std::cout << "constructions (3): ";
// allocates storage for five objects, but does not construct them:
MyClass * p3 = static_cast<MyClass*> (::operator new (sizeof(MyClass[5])));
std::cout << '\n'; std::cout << "constructions (4): ";
// constructs five objects at p3, but does not allocate:
new (p3) MyClass[5];
std::cout << '\n'; delete[] p3;
delete[] p2;
delete[] p1; return 0;
}
 
 

关于operator delete和operator new  ,可以参看这里的原型定义:

http://en.cppreference.com/w/cpp/memory/new/operator_delete

http://en.cppreference.com/w/cpp/memory/new/operator_new

可以看到,都是void*型的指针,跟C语言里面的malloc  free机制差不多了。

C++中的::operator new, ::operator delete的更多相关文章

  1. C++中的new/delete与operator new/operator delete

    new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数. new operator(1)调用opera ...

  2. C++ new operator, delete operator, operator new, operator delete, new placement

    http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...

  3. 小结:c++中的new、operator new和placement new

    小结:c++中的new.operator new和placement new new(也称作new operator),是new 操作符,不可重载 class T{...}; T *t = new T ...

  4. c++中的new、operator new、placement new

    一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...

  5. C++ 中 new 操作符内幕:new operator、operator new、placement new

    一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...

  6. C++中的new、operator new与placement new

    转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...

  7. C++内存管理(new operator/operator new/operator delete/placement new)

    new operator 我们平时使用的new是new操作符(new operator),就像sizeof一样是语言内置的,不能改变它的含义,功能也是一样的 比如: string *ps = new ...

  8. 浅谈new operator、operator new和placement new 分类: C/C++ 2015-05-05 00:19 41人阅读 评论(0) 收藏

    浅谈new operator.operator new和placement new C++中使用new来产生一个存在于heap(堆)上对象时,实际上是调用了operator new函数和placeme ...

  9. 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new

    引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...

随机推荐

  1. jquery.validate的 使用

    需要引入jar包 <script type="text/javascript" src="js/jquery-1.10.2.min.js">< ...

  2. node.js关于传送数据的二三事

    配置好node环境后 书写代码 目录结构: . 代码: <!DOCTYPE html> <html lang="en"> <head> < ...

  3. jmeter 监听的介绍

    一个侦听器是一个组件,显示的结果 样本. 结果可以显示在一个树,表格,图表或简单地写入到日志中 文件. 查看的内容反应任何给定的采样器,添加的监听器” 视图 结果树 ”或“ 视图的结果表 一个测试计划 ...

  4. Android存储数据方式

    可以查看Android开发文档中的:/docs/guide/topics/data/data-storage.html Android provides several options for you ...

  5. tab切换-自动、点击、内容变换

    <div class="tab">                    <ul class="pics">               ...

  6. Play framework logging设置

    play的logger是基于Log4j,Play 2.0 uses logback as its logging engine. 一.配置 1. 在conf/application.conf中设置lo ...

  7. SVN clean up问题

    问题如截图所示: 解决方法:在根目录clean up,注意把所有的选项都勾上

  8. econ

    1) If Ep > 1, Demand is elastic. 2) If Ep < 1, Demand is inelastic 3) If Ep = 1, Demand has un ...

  9. 解决apache AH01630: client denied by server configuration错误

    昨天给公司配置了apache-2.4.9的版本,今天他们要求把虚拟主机配置起好放网站程序,在修改apache-2.4.9的配置文件中,我发现了2.4.x跟以前的2.2.x里面的很多配置都不一样了,比如 ...

  10. C#语法小用法

    数据在存为数据库之前,用JS的encodeURIComponent进行编码,现需要在后台代码中进行解码,实现decodeURIComponent的功能, 如下: HttpUtility.UrlDeco ...