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

鍵盤上的舞者

My Email: marckywu@gmail.com
隨筆 - 19, 文章 - 0, 評(píng)論 - 3, 引用 - 0
數(shù)據(jù)加載中……

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 閱讀(389) 評(píng)論(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>
            亚洲国产第一| 最近看过的日韩成人| 亚洲午夜电影| 国产精品a久久久久久| 一区二区三区三区在线| 亚洲人成在线影院| 欧美日本精品一区二区三区| 日韩视频精品| 在线天堂一区av电影| 国产裸体写真av一区二区| 久久久久欧美精品| 久久蜜桃精品| 亚洲视频欧美视频| 欧美一区观看| 亚洲精品在线视频| 中文有码久久| 精品成人久久| 亚洲精品国产精品乱码不99| 国产精品国产三级国产专播精品人| 亚洲一区视频在线观看视频| 性伦欧美刺激片在线观看| 亚洲第一在线视频| 在线视频欧美日韩精品| 国内视频一区| 亚洲人体大胆视频| 国产日韩欧美一二三区| 欧美成人午夜激情视频| 欧美日韩免费视频| 久久亚洲国产精品一区二区| 欧美另类高清视频在线| 欧美伊人精品成人久久综合97| 久久精品亚洲乱码伦伦中文| 日韩视频在线观看国产| 亚洲欧美日韩国产中文在线| 亚洲国产美国国产综合一区二区| 一区二区精品| 亚洲欧洲综合另类| 性做久久久久久久免费看| 亚洲精选在线观看| 午夜欧美大片免费观看| 日韩视频永久免费观看| 欧美伊人久久| 午夜精品网站| 欧美精品在线观看91| 久久综合九色综合网站| 国产精品久久久久久五月尺| 亚洲电影毛片| 在线播放中文字幕一区| 午夜在线a亚洲v天堂网2018| 一本色道久久综合狠狠躁的推荐| 久久美女性网| 久久久国际精品| 国产欧美日韩麻豆91| 一区二区三区欧美成人| 99精品国产高清一区二区| 久久视频在线看| 久久综合中文字幕| 韩国欧美国产1区| 亚洲一区二区三区激情| 亚洲新中文字幕| 欧美日本高清| 亚洲精品在线一区二区| 亚洲国产美女精品久久久久∴| 久久成人一区| 久久精品综合网| 国产有码一区二区| 欧美一区二区三区的| 久久精品伊人| 狠久久av成人天堂| 久久久久久久综合日本| 久久天堂成人| 在线精品视频免费观看| 麻豆精品视频| 亚洲精品国产精品国自产在线| 亚洲精品乱码久久久久久| 欧美风情在线观看| 最近中文字幕日韩精品| 正在播放日韩| 国产精品区免费视频| 亚洲在线观看视频| 久久高清一区| 亚洲第一视频网站| 欧美电影免费观看高清| 亚洲日本一区二区| 亚洲一区二区3| 国产欧美日韩视频在线观看| 久久精品国产一区二区三区| 久久久久久九九九九| 亚洲成色777777女色窝| 欧美成人a视频| 宅男噜噜噜66一区二区66| 欧美在线观看视频一区二区| 一区二区三区在线免费视频| 麻豆精品在线视频| 日韩亚洲在线观看| 久久久久久久久久久一区 | 欧美日韩国产va另类| 中国成人在线视频| 久久久欧美一区二区| 亚洲精品免费在线| 国产精品免费看| 久久久久免费观看| 日韩视频二区| 老色批av在线精品| 亚洲视频视频在线| 黄色精品一区| 欧美日韩一区视频| 久久欧美肥婆一二区| 亚洲人成亚洲人成在线观看图片| 亚洲欧美日韩国产一区二区| 在线观看欧美亚洲| 国产精品久在线观看| 乱中年女人伦av一区二区| 亚洲最新色图| 亚洲第一色中文字幕| 欧美中文在线观看国产| 亚洲老司机av| 在线欧美不卡| 国产精品视频大全| 欧美日韩成人在线播放| 久久久久久高潮国产精品视| 中文亚洲免费| 亚洲激情第一区| 免费成人小视频| 久久精品人人做人人爽| 亚洲欧美日韩精品久久久| 亚洲人线精品午夜| 国内自拍视频一区二区三区| 国产精品久久7| 欧美日韩久久久久久| 美女久久一区| 久久久人成影片一区二区三区观看 | 激情另类综合| 国产午夜精品美女毛片视频| 国产精品成人观看视频免费| 欧美国产激情| 欧美高清免费| 欧美成人一区二区三区片免费| 欧美一区二区三区久久精品| 亚洲一区二区三区四区视频| 日韩视频免费在线| 亚洲国产精品久久久久婷婷老年| 欧美jizzhd精品欧美巨大免费| 久久青草久久| 免费亚洲电影在线观看| 免费不卡在线观看| 免费av成人在线| 欧美成人精品1314www| 另类欧美日韩国产在线| 久久综合色播五月| 久久亚洲国产精品日日av夜夜| 久久婷婷国产综合精品青草 | 伊人一区二区三区久久精品| 精品51国产黑色丝袜高跟鞋| 狠狠色噜噜狠狠狠狠色吗综合| 国际精品欧美精品| 在线精品观看| 99riav久久精品riav| 在线一区免费观看| 午夜精品国产更新| 久久aⅴ乱码一区二区三区| 久久精品五月婷婷| 美乳少妇欧美精品| 亚洲国产精品一区二区尤物区| 亚洲人精品午夜| 亚洲综合久久久久| 香蕉尹人综合在线观看| 久久久国产91| 欧美日韩国产91| 国产欧美韩日| 亚洲国产高清自拍| 一区二区三区高清在线| 欧美一级久久| 欧美黑人在线播放| 日韩一区二区电影网| 欧美亚洲免费电影| 欧美二区在线| 国产亚洲精品高潮| 91久久精品国产91性色tv| 亚洲少妇在线| 久久一区二区三区超碰国产精品| 亚洲大黄网站| 亚洲免费在线| 嫩草国产精品入口| 国产美女精品视频免费观看| 亚洲全部视频| 久久9热精品视频| 亚洲精品欧美在线| 久久精品国产99精品国产亚洲性色 | 亚洲一区二区av电影| 久久国产精品久久久久久| 欧美极品在线观看| 国产日韩欧美不卡在线| 99国内精品久久久久久久软件| 久久国产88| 国产精品99久久久久久有的能看 | 欧美一区三区二区在线观看| 欧美激情一二三区| 怡红院精品视频在线观看极品| 亚洲性xxxx|