C++易混知识点3. New Operator, Operator New, Placement New 实例分析,比较区别
来源:互联网 发布:运营商云计算 编辑:程序博客网 时间:2024/06/07 02:03
我们知道,C++中引入了New 这个内置符号,很大方便了指针的使用,程序员不必关注与这块堆上新分配的内存是如何来的,如何初始化的,然后如何转换为我们想要的类型指针的。现在,我们重点来分析下这个NEW内置符号背后的步骤和所调用到的函数。这里面涉及到new operator, operator new, placement new.
转载请注明出处: http://blog.csdn.net/elfprincexu
1. New Operator (__type__ * a = new a();)
C 语言当中,我们在新申请一块内存的时候,通常使用的是malloc, 通常通过malloc(size_t size)来获得一块未初始化的raw memory。然后一般情况下我们会对该部分空间初始化(memset or memcpy)。
C++语言引入了new 操作符,极大地方便了使用,它一下子把C语言中的好几个步骤一下完成了,让我们觉得神奇的是,不必去深究其中的调用。
其实,new operator主要做了以下三个步骤的:
std::string* str = new std::string("memory management");// 例如,我们想申请一个新的string空间,其中空间并被初始化为“memory management”,并且该空间由一个stringlei类型的指针来指向,并把它返回给str。
1. 第一步,调用了 operator new 函数, operator new函数的作用 仅仅 是申请一块未被初始化的内存。
函数申明为:void* operator new (size_t size)
void* memory = operator new (sizeof("memory management"));// 申请足够大小的内存空间
2. 第二部,调用string 构造函数,在该未初始化的空间上初始化,也就是“memory management”
call string::string("memory management") on memory*
3. 第三部,将该指针转换为指定的想要的指针类型
string* str = static_cast<std::string*> (memory);
总结下来:
(1)通过operator new申请内存
(2)使用placement new调用构造函数(简单类型忽略此步)
(3)返回内存指针
2. operator new (该函数仅仅 申请足够大小的内存,可以被重载)
void * operator new (size_t size);
举个例子来讲:
// operator new example#include <iostream>#include <new>using namespace std; struct myclass {myclass() {cout <<"myclass constructed\n";}}; int main () { int * p1 = new int;// same as:// int * p1 = (int*) operator new (sizeof(int)); int * p2 = new (nothrow)int;// same as:// int * p2 = (int*) operator new (sizeof(int),nothrow); myclass * p3 = (myclass*) operator new (sizeof(myclass));// (!) not the same as:// myclass * p3 = new myclass;// (constructor not called by function call, even for non-POD types) new (p3) myclass; // calls constructor// same as:// operator new (sizeof(myclass),p3) return 0;}
重载operator new,
void * operator new (size_t size, string str);// 比如我们添加一个形参string, 用来辅助输出
调用
Base *b = new (“Operator new overload example”) Base;// str 形参放在new 后的挂号里面,
same as :
Base *b = (Base* ) ( operator new (sizeof(Base), "Operator new overload example") );
class Base {public: Base() { } void *operatornew(size_t size, string str ) { cout << "Logging an allocation of " << size << " bytes for new object'" << str << "'" << endl; return malloc( size ); } int var; double var2;}; Base* b = new ("Base instance 1") Base;
输出: Logging an allocation of 12 bytes for new object 'Base instance 1'
3. Placement new. void* operator new ( size_t size, void* ptr) ;// ptr 会被返回,因为我们已经指定在已知的内存上
归根结底来讲,placement new 是operator new 的一种特殊重载,它不分配内存,只是调用合适的构造函数在ptr所指的地方构造一个对象,之后返回实参指针ptr。
例如:
myclass* p3 = static_cast<myclass*> ( operator new (sizeof(myclass));// p3 已经指定一块足够放下myclass对象的空间,该空间未初始化,
new (p3) myclass;// placement new, 在指定的p3空间上,call constructor myclass()来初始化该空间,并且返回p3.
// operator new example#include <iostream>#include <new>using namespace std; struct myclass {myclass() {cout <<"myclass constructed\n";}}; int main () { myclass * p3 = (myclass*) operator new (sizeof(myclass));// (!) not the same as:// myclass * p3 = new myclass;// (constructor not called by function call, even for non-POD types) new (p3) myclass; // calls constructor// same as:// operator new (sizeof(myclass),p3) return 0;}
- C++易混知识点3. New Operator, Operator New, Placement New 实例分析,比较区别
- new 、operator new 和 placement new 区别
- new 、operator new 和 placement new 区别
- new 、operator new 和 placement new 区别
- placement new operator new
- new; operator new; placement new
- new, operator new, placement new
- new、operator new、placement new
- new、operator new、placement new
- new,operator new,placement new
- placement new, operator new and new operator
- placement new, operator new and new operator
- placement new, operator new and new operator
- 关于new operator, operator new, placement new
- placement new, operator new and new operator
- operator new,new operator, placement new
- Placement new、operator new、new operator
- new operator /operator new 和placement new
- Android数据库高手秘籍(一)——SQLite命令
- golang中的use-a和has-a
- NYOJ 题目63小猴子下落
- 【Android 一般进阶】ListView滑动到顶部smoothScrollToPosition
- oracle的正则表达式
- C++易混知识点3. New Operator, Operator New, Placement New 实例分析,比较区别
- 加载由mxml生成的swf,并访问内部方法
- Target runtime Apache Tomcat v5.5 is not defined
- 打印Log的工具类
- primer 1112
- Android--六款值得推荐的android(安卓)开源框架简介
- 支付宝调试错误,错误代码 ILLEGAL_SIGN的解决
- 改变spring-servlet.xml名字和默认位置
- SDUT 1569 选夫婿1