关于placement new 和 placement delete的重载,以及basic_string重载new()实例

关于placement new

在https://blog.csdn.net/qq_42604176/article/details/111997397中已经介绍了placement new的形式。
它的形式为new()/delete().我们将分配好内存的指针送入括号中,就完成了初步的调用了。
其实我们可以定义放任何的东西到()内部。只放一个指针的版本是的new()是标准库先写好给我们的。
我们可以重载operator new,并写出多个版本,如:

Foo* pf = new(300,'c')Foo;	//注意,这里没有传入指针

前提是每一个版本的声明都必须由独特的参数列,其中第一个参数必须是size_t,这是因为当没有()时,进行的是new Foo操作,Foo的大小会被传进operator new中作为第一参数,Foo的大小是个size_t类型。所以我们写的各种各样的版本也必须遵循这个规则。第二第三参数等等可由自己设计。new()括号中的就是第二第三参数,他们可以指定placement arguments 为初值。
下面是实例:

class Foo {
public:Foo() {cout << "Foo::Foo()" << endl; };Foo(int) {cout << "Foo::Foo()" << endl; throw Bad();}	//这里故意抛出异常,用来测试 placement operator delete//【1】一般的operator new()的重载void* operator new(size_t size) {return malloc(size);}//【2】这个是标准库已提供的placement new()的重载形式void* operator new(size_t size, void* start) {return start;}//【3】这个是我们重载的 placement new void* operator new(size_t size, long extra) {return malloc(size + extra);}//【4】这个也是我们重载的 placement newvoid* operator new(size_t size, long extra, char init) {return malloc(size + extra);}//【5】这个也是我们重载的,不过我们故意写错定义参数的类型void* operator new(long extra, char init) {return malloc(extra);}	//很显然这个版本会报错
};

关于placement delete

我们也可以重载placement operator delete,并对应着placement operator new写出多个对应版本,但他们绝对不会被delete调用。
只有当new所调用的ctor抛出异常,才会调用这些重载版本的operator delete。
也就是说重载的placement operator delete是用来释放未能成功创建的对象所占的内存。(正如我们所知,创建一个对象实际上是先申请空间,再调用构造函数。空间申请到了,但是对象却没构造出来,那么理所当然需要将空间释放)
对应上面的四种版本的delete:

//【1】一般的 operator delete()的重载
void operator delete(void*,size_t)
{cout << "operator delete(void*,size_t)" << endl;
}
//【2】对应第二种
void operator delete(void*,void*)
{cout << "operator delete(void*,void*)" << endl;
}
//【3】对应第三种
void operator delete(void*,long)
{cout << "operator delete(void*,long)" << endl;
}
//【4】对应第四种
void operator delete(void*,long,char)
{cout << "operator delete(void*,long,char)" << endl;
}

侯捷老师给出了下面的示例,运行到第五种。我们可以发现,此时的构造函数调用的是第二种构造函数。在之前的定义中,我们在这里抛出了异常。
在这里插入图片描述
接下俩便是这几条语句的执行结果:
在这里插入图片描述
如上所示,这些new都被重载了。所以才会打印信息。
按照道理,在构造函数抛出异常后,会调用自己重载的placement delete,打印信息。但在这里并没有,这是编译器的原因。

关于basic_string重载new()来扩充申请量

basic_string是标准库里面的一个class,就是我们使用的字符串。
如下:

template<...>
class basic_string
{
private:struct Rep {...};...void release() {if(--ref == 0) delete this;}inline static void* operator new(size_t,size_t);inline static void operator delete(void*);inline static Rep* create(size_t);...
};

operator new的具体代码如下:

template<class charT,class traits, class Allocator>
inline void* basic_string<charT,traits,Allocator>::Rep::
operator new(size_t s,size_t extra)
{return Allocator::allocate(s + extra * sizeof(charT));
}

如何使用看这儿:
这里我们把第二参数叫做extra。它的作用是,当使用者去创建一个字符串,如"hello",加上结束符一共6个字符。但是它在分配的时候还会分配extra个字符大小的空间。具体原因不做细究。

template<class charT,class traits,class Allocator>
inline basic_string<charT,traits,Allocator>::Rep*
basic_string<charT,traits,Allocator>::Rep::
create(size_t extra)
{extra = frob_size(extra + 1);Rep *p = new(extra)Rep;...return p;
}

它的placement delete重载之后则长这样:

template<class charT,class traits,class Allocator>
inline void basic_string<charT,traits,Allocator>::Rep::
operator delete(void* ptr)
{Allocator::deallocate(ptr,sizeof(Rep) + reinterpret_cast<Rep*>(ptr)->res * sizeof(charT));
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377367.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cwc云莱特链_CWC的完整形式是什么?

cwc云莱特链CWC&#xff1a;工作条件的改变 (CWC: Change in Working Conditions) CWC is an abbreviation of "Change in Working Conditions". CWC是“工作条件更改”的缩写 。 It is an expression, which is commonly used in the Gmail platform. In a particu…

在eclipse中创建web项目(非myeclipse)

在eclipse中创建web项目(非myeclipse) 在eclipse中如何创建dynamic web project项目 本文的演示是从本地文件创建dynamic web project&#xff0c;从svn检出的同时创建dynamic web project于此类似。我们推荐使用解压版的tomcat6.x版本&#xff0c;来作为服务器。可以到http://…

opengl glut 编译

新建工程glut dll工程&#xff0c;本来想创建lib,工程的&#xff0c;但是想起来&#xff0c;gl是状态机机制。dll方便资源共享等。 添加两个include目录 各种手机电脑平台&#xff0c;网络多媒体开发,mmsplayer&#xff0c;QQ514540005 然后将目录下的lib/glut下面所有的.c文件…

划分数据集代码(按照4:1的比例)以及根据各自文件名写入txt文件

会将图片分到两个文件夹中&#xff1a; #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #incl…

bpo是什么意思_BPO的完整形式是什么?

bpo是什么意思BPO&#xff1a;业务流程外包 (BPO: Business Process Outsourcing) BPO is an abbreviation of Business process outsourcing. It is a convention of a company to another company which is an external provider of services or business operations process…

在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制

在进行 ASP.NET 开发时&#xff0c;有时候需要对页面输出的最终 HTML 源代码进行控制&#xff0c;是页面的 render 方法中很容易实现这个功能。下面就是一个实现的方法&#xff0c;注释都在代码中。 [c-sharp] view plaincopy <% Page Language"C#" %> <% …

FireFox插件SQLite Manager的使用

最近几天开始高IOS数据库来着&#xff0c;一开始就CoreData的学习&#xff0c;结果高了一天没有一点进展。 没法&#xff0c;还是先老实代码着吧&#xff0c;不过用的火狐插件可视化数据库的操作也是不错的似乎。 网上搜了搜用法&#xff0c;还真没找到有什么的&#xff0c;最后…

简单的数据增强代码(C++与opencv)

包括了图片批量平移、旋转、以及像素变换 #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #in…

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高级加密标准 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法详解

这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用. //这里创建一个圆角矩形的按钮UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能够定义的button类型有以下6种&#xff0c;// typedef enum {// UIButtonTypeCusto…

针对一个class写出它的内存管理池以及总结出allocator类(三个版本)

目录示例版本1&#xff1a;per-class allocator,1示例版本2&#xff1a;per-class allocator,2最终版本&#xff1a;static allocator针对版本三进行macro如果我们不针对对象做内存管理&#xff0c;那么我们每次进行Foo* p new Foo(x);时总是会调用malloc函数。 尽管malloc函数…

kotlin 第一个程序_Kotlin程序添加两个矩阵

kotlin 第一个程序Given two matrices, we have to add them. 给定两个矩阵&#xff0c;我们必须将它们相加。 Example: 例&#xff1a; Input:matrix 1:[2, 3][4, 5][7, 1]matrix 2:[4, 6][9, 0][7, 6]Output:[6, 9][13, 5][14, 7] 在Kotlin中添加两个矩阵的程序 (Progra…

ubuntu 切换用户的命令[shell, linux]

使用ubuntu过程中免不了和shell(终端)打交道, 也不可避免在各种用户之间进行切换, 从而实现对各帐户的管理, 这个就涉及到了一个比较基础又很重要的工作,怎么样切换用户, 对于LINUX老鸟来说,这个根本不值不提的东东却让新手挠头不已, 现在给出普通用户和超级用户切换的命令(附图…

曲苑杂坛--修改数据库名和文件组名

/* 该脚本示例如何完整的修改一个数据库的名称. 数据库为原名称为DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判断是否存在同名的数据库&#xff0c;以防止误删除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

关于new handler与default、delete关键字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代码长啥样中谈到过new handler。 当operator new不能够分配出申请的内存时&#xff0c;会抛出bad_alloc 异常。有的编译器会返回0. 当定义成new(nothrow) Foo&#xff1b;就不会抛异常&#xff…

模式匹配运算符–Shell

转载&#xff1a;http://www.firefoxbug.net/?p722 Var/home/firefox/MyProgram/fire.login.name ${Variable#pattern}:如果模式匹配于变量值的开头处&#xff0c;则删除匹配的最短部分&#xff0c;并且返回剩下的部分 例子&#xff1a; [fire]$ echo ${Var#*/} [fire]$ home/…

河内塔问题_河内塔的Python程序

河内塔问题You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter o…

VC6、BC5、G2.9标准分配器一览

目录VC6标准分配器BC5标准分配器G2.9标准分配器VC6标准分配器 VCx中源码可以在电脑路径中找&#xff1a; [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不过太多了。大概在837行左右有关于allocator代码。还是先看侯捷PPT上的吧。 …

【转】shell 大括号、圆括号的使用

在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量&#xff0c;命令的用法&#xff0c;如下&#xff1a; PHP 代码:1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:string},${var:string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(va…

css clear属性_CSS中的clear属性

css clear属性CSS | 清除财产 (CSS | clear Property) We know so much about float property and how it is used for styling our web pages. If you do not remember the float property, lets help jog your memory. The float property is used to set the elements in a …