青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

鍵盤上的舞者

My Email: marckywu@gmail.com
隨筆 - 19, 文章 - 0, 評論 - 3, 引用 - 0
數據加載中……

Allocating Arrays Using Placement new (zz)

       An additional version of operator new enables you to construct an object or an array of objects at a predetermined memory position. This version is called placement new and has many useful applications, including building a custom-made memory pool or a garbage collector. Additionally, it can be used in mission-critical applications because there's no danger of allocation failure; the memory that's used by placement new has already been allocated. Placement new is also faster because the construction of an object on a preallocated buffer takes less time.

You already know how to use placement new to allocate a single object on a predetermined memory address. However, some programming tasks require the allocation of arrays on a predetermined memory address. Here's how you do it.

Placement new Overview

Mobile devices, embedded systems and custom garbage collectors are only a few instances of programming environments that may require placement new allocation of arrays. Before I discuss the details of such array allocations, let's remind ourselves briefly how scalar (i.e. non-array) placement new works.

The scalar version of placement new takes a user-supplied address on which it constructs a single object. Unlike the ordinary version of the new operator, placement new doesn't allocate storage for the object; it merely constructs the object on the memory address you provide:

#include <new> //required for using placement new
class Widget {
public:
    Widget();
    
virtual ~Widget
    
virtual void Draw();
};
char* buf=new char [sizeof (Widget)];//preallocate
Widget* widget= new(buf) Widget; //construct Widget on buf
widget->Draw(); //use Widget

To destroy widget you first have to invoke its destructor explicitly:

widget->~Widget(); //explicit destructor invocation

Next, reclaim the raw memory like this:

delete[] buf;

Array Allocation

Allocating arrays with placement new follows the same steps more or less, but you have to pay attention to additional nuances. Here is a step-by-step guide:

First, allocate a buffer large enough to hold an array of the desired type:

const int ARRSIZE = 15;
char * buf= new [sizeof(Widget)*ARRSIZE];

Don't be tempted to calculate the size manually; always use sizeof to ensure that the buffer is properly aligned and has the right size.

Next, construct an array of ARRSIZE objects on the buffer using placement new[] :

Widget* widgets=new(buf) Widget[ARRSIZE];//construct an array

You can now use the allocated array as usual:

for (int i=0; i<ARRSIZE; i++)
{
 widgets[i].Draw();
}
Make sure that your target class -- Widget in this example -- has a public default constructor. Otherwise, it would be impossible to create arrays thereof.

Destroying the Array

To destroy such an array allocated by placement new you have to call the destructor for each element explicitly:

int i=ARRSIZE;
while (i)
    widgets[
--i].~Widget();

The while -loop uses a descending order to preserve the canonical destruction order of C++ -- the object that was constructed last must be destroyed first. To comply with this requirement, the element with the highest index is destroyed first.

Finally, you release the raw memory on which the array resided by calling delete[] :

delete[] buf;

Performance Tuning

The array placement new has a potential performance problem: it initializes every element in the array unconditionally. If your app deals with large arrays, this isn't the most efficient way. In some apps only a portion of the array is actually used, and in other apps the elements are assigned a different value immediately after their construction. In these cases, you want to postpone, or even completely avoid, the automatic initialization of array elements. To avoid the initialization of placement new arrays, follow the following steps:

As before, begin with an allocation of a raw buffer with the appropriate size. This time however, use the global operator new instead of the new operator:

Widget * warr=
static_cast
<Widget*> (::operator new ( sizeof(Widget)* ARRSIZE));

The global operator new , very much like C's malloc() , merely allocates raw bytes of memory from the free-store, without initializing them. It returns void * rather than Widget* which is why you need to cast the result explicitly.

At this stage, warr is a pointer to raw memory. You can't access its elements because they haven't been initialized yet. To initialize individual elements, call placement new once more, for each element you want initialized:

void assign(Widget arr[], size_t & sz,  const Widget& init)
{
    
new (&arr[sz++]) Widget (init); //invoke copy ctor
}

assign() passes the address of an individual element to placement new which in turn invokes Widget 's copy constructor. The copy-constructor initializes that element with init . Using this technique, you can initialize elements selectively, leaving the rest of the array uninitialized.

To destroy such an array, invoke the destructor of every initialized object. Then call the global operator delete to reclaim the raw storage:

void destroy(Widget arr[], size_t & sz)
{
    
while (sz)
    {
        arr[
--sz].~Widget();//destroy all initialized elements
    }
     ::
operator delete (arr); //reclaim raw storage
}

Summary

The techniques I've presented here are bug prone. Therefore, they should be encapsulated in higher-level classes that hide the implementation details from users. These techniques aren't rarely-used as they might seem. STL allocators use them under the hood to avoid object initialization and minimize reallocations.



posted on 2009-08-13 00:48 Marcky 閱讀(379) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            91久久精品久久国产性色也91| 亚洲人成在线观看一区二区 | 亚洲精品网址在线观看| 国产精品一区二区三区乱码| 亚洲精品视频在线看| 在线欧美亚洲| 久久国产精品亚洲77777| 午夜日本精品| 国产精品日韩高清| 夜夜爽99久久国产综合精品女不卡| 亚洲欧洲精品一区二区三区波多野1战4 | 午夜精品久久久久久| 欧美噜噜久久久xxx| 亚洲国产91| 最新国产成人在线观看 | 亚洲欧美美女| 欧美一区二区三区在| 国产精品免费小视频| 亚洲淫片在线视频| 欧美一级一区| 国产一区二区精品久久91| 午夜日韩在线| 久久天堂国产精品| 激情欧美一区二区| 老司机午夜精品视频在线观看| 美女爽到呻吟久久久久| 亚洲国产精品123| 欧美aⅴ99久久黑人专区| 亚洲国产裸拍裸体视频在线观看乱了| 在线精品亚洲一区二区| 老司机精品视频网站| 91久久久久| 亚洲永久网站| 国产一区二区中文| 美女被久久久| 99ri日韩精品视频| 欧美伊人精品成人久久综合97| 国产一区二区三区日韩欧美| 久久青草福利网站| 亚洲精品国产欧美| 亚洲欧美综合网| 黄网站免费久久| 欧美激情一区二区三区四区| av不卡在线| 久久久夜色精品亚洲| 亚洲三级观看| 欧美性大战久久久久久久| 亚洲欧美激情四射在线日| 美日韩精品视频| 一本色道久久88综合亚洲精品ⅰ| 国产精品久久中文| 久久综合精品国产一区二区三区| 亚洲经典视频在线观看| 午夜电影亚洲| 亚洲日本视频| 国产精品一级久久久| 鲁大师影院一区二区三区| 亚洲美女av网站| 久久久久久久高潮| 一区二区三区.www| 精品91在线| 国产精品久久久久久久电影| 久久躁日日躁aaaaxxxx| 亚洲图片欧美日产| 亚洲国产精品毛片| 久久精品九九| 亚洲四色影视在线观看| 亚洲观看高清完整版在线观看| 国产精品久久97| 欧美成人免费一级人片100| 午夜精品免费| 一本色道久久99精品综合| 免费在线欧美视频| 久久精品国产综合| 亚洲在线网站| 夜夜嗨av一区二区三区| 亚洲大黄网站| 国产综合视频| 国产精品日日摸夜夜添夜夜av| 欧美不卡在线视频| 久久久蜜桃精品| 欧美一二区视频| 一区二区三区|亚洲午夜| 欧美激情小视频| 久久综合免费视频影院| 欧美一区二区三区久久精品茉莉花 | 亚洲成色999久久网站| 国产亚洲激情在线| 国产九色精品成人porny| 欧美午夜精品久久久久久超碰| 暖暖成人免费视频| 久久综合亚州| 久久久午夜精品| 久久av一区二区三区亚洲| 亚洲一区二区在线| 一区二区免费在线视频| 99精品国产在热久久| 亚洲日韩欧美视频一区| 亚洲国产影院| 亚洲美女在线看| 一本色道精品久久一区二区三区| 亚洲精品1区| 亚洲精品免费电影| 亚洲精品一区二区三区av| 亚洲黄色性网站| 亚洲欧洲一区二区在线播放 | 亚洲国产小视频在线观看| 亚洲电影网站| 亚洲精品自在久久| 一本一本a久久| 亚洲尤物精选| 欧美一区二区三区免费大片| 欧美伊人久久| 久久琪琪电影院| 美女国产精品| 欧美人与禽猛交乱配| 欧美性做爰毛片| 国产精品丝袜白浆摸在线| 国产一区二区三区观看 | 91久久国产综合久久91精品网站| 亚洲美女视频在线观看| 一区二区三区www| 翔田千里一区二区| 久久久久久久一区二区三区| 蜜臀久久99精品久久久久久9| 欧美韩日一区| 亚洲理伦在线| 亚洲欧美日韩天堂一区二区| 久久久精品2019中文字幕神马| 媚黑女一区二区| 欧美三级视频在线| 国产亚洲一区二区三区| 亚洲黄色大片| 午夜国产精品影院在线观看| 久久只精品国产| 91久久在线播放| 亚洲欧美成人一区二区在线电影| 久久精品1区| 欧美日韩精品免费观看视一区二区| 国产精品久久久久aaaa| 精品成人一区| 在线一区日本视频| 麻豆av一区二区三区久久| 日韩视频免费| 久久久亚洲精品一区二区三区| 欧美日韩日本网| 亚洲福利视频二区| 欧美亚洲自偷自偷| 亚洲欧洲在线一区| 欧美一区二区日韩| 欧美丝袜一区二区三区| 亚洲国产精品免费| 久久爱www久久做| 99riav1国产精品视频| 另类亚洲自拍| 国产农村妇女精品| 亚洲深夜福利视频| 亚洲高清视频一区二区| 欧美一区二区三区在线免费观看| 欧美激情久久久久| 在线观看欧美成人| 久久精品一区中文字幕| 在线亚洲一区二区| 欧美电影免费观看大全| 激情国产一区| 欧美在线免费观看| 在线亚洲伦理| 欧美日韩国产区一| 亚洲国产精品一区| 鲁大师成人一区二区三区| 亚洲一区免费看| 欧美三级在线视频| 一区二区三区 在线观看视频| 欧美福利一区二区| 久久婷婷激情| 激情欧美国产欧美| 久久视频一区| 欧美专区在线播放| 国产一区深夜福利| 久久久国产精品一区二区中文| 这里只有精品视频在线| 欧美日韩中文字幕在线| 一本到高清视频免费精品| 欧美激情一区二区| 久久综合成人精品亚洲另类欧美| 国产亚洲欧美日韩日本| 久久成人精品无人区| 亚洲综合清纯丝袜自拍| 国产精品美女久久久浪潮软件| 亚洲一区二区在线看| 在线一区欧美| 国产精品自拍小视频| 午夜综合激情| 香蕉av福利精品导航| 国产综合欧美| 欧美波霸影院| 欧美精品一区二区三区久久久竹菊 | 亚洲欧美资源在线| 国产日韩欧美一区二区三区四区| 欧美专区日韩视频|