Item 35. Placement New

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

Item 35. Placement New

看起来不可能的事情在C++中总能找到解决的办法。

正如,直接调用构造函数是不可能的,然而可以用placement new 欺骗编译器调用构造函数。

--------------------------------------------------

1、placement new 为何物?

placement new 是重载operator new 的一个标准、全局的版本,它不能够被自定义的版本代替(不像普通版本的operator new 和 operator delete能够被替换)。

void *operator new( size_t, void *p ) throw()

{ return p; }

placement new的执行忽略了size_t参数,只返还第二个参数。其结果是允许用户把一个对象放到一个特定的地方,达到调用构造函数的效果。

class SPort { ... }; // represents a serial port

const int comLoc = 0x00400000; // location of a port

//...

void *comAddr = reinterpret_cast<void *>(comLoc);

SPort *com1 = new (comAddr) SPort; // create object at comLoc

com1->~SPort(); //释放

2、new 、operator new 和 placement new 一样吗?

new :不能被重载,其行为总是一致的。它先调用operator new分配内存,然后调用构造函数初始化那段内存。

operator new:要实现不同的内存分配行为,应该重载operator new,而不是new。

delete和operator delete类似。

placement new:只是operator new重载的一个版本。它并不分配内存,只是返回指向已经分配好的某段内存的一个指针。因此不能删除它,但需要调用对象的析构函数。

3、在已有的内存上用placement new分配数组

const int numComs = 4;

//...

SPort *comPorts = new (comAddr) SPort[numComs]; // create array

int i = numComs;

while( i )

comPorts[--i].~SPort();

4、用Placement new 解决buffer的问题

用new分配的数组缓冲时,由于调用了默认构造函数,因此执行效率上不佳。若没有默认构造函数则会发生编译时错误。用Placement new可以解决此类问题。

const size_t n = sizeof(string) * BUFSIZE;

string *sbuf = static_cast<string *>(::operator new( n ));

int size = 0;

//此时,buffer还没有初始化,因此需要用 placement new 调用copy构造函数初始化。

void append( string buf[], int &size, const string &val )

{ new (&buf[size++]) string( val ); } // placement new

//最后的清理

void cleanupBuf( string buf[], int size ) {

while( size )

buf[--size].~string(); // destroy initialized elements

::operator delete( buf ); // free storage

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有