亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 關(guān)于operator new和operator new[]的區(qū)別 [打印本頁]

作者: liwangli1983    時(shí)間: 2011-01-30 21:25
標(biāo)題: 關(guān)于operator new和operator new[]的區(qū)別
如題,new和new[]的區(qū)別比較明顯了,主要是new[]會(huì)多次調(diào)用構(gòu)造函數(shù),依次初始化對(duì)象。

但operator new和operator new[]不都是單純的返回一個(gè)指向heap的void*指針么?有什么區(qū)別呢?請(qǐng)大家指教,謝謝!
作者: 幻の上帝    時(shí)間: 2011-01-31 04:26
ISO C++03 5.3.4
10 A new-expression passes the amount of space requested to the allocation function as the first argument of type std::size_t. That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array. For arrays of char and
unsigned char, the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the most stringent alignment requirement (3.9) of any object type whose size is no greater than the size of the array being created. [Note: Because allocation functions are assumed to return pointers to storage that is appropriately aligned for objects of any type, this
constraint on array allocation overhead permits the common idiom of allocating character arrays into which
objects of other types will later be placed. ]

12 [Example:
— new T results in a call of operator new(sizeof(T)),
— new(2,f) T results in a call of operator new(sizeof(T),2,f),
— new T[5] results in a call of operator new[](sizeof(T)*5+x), and
— new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).
Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operator new[]. This overhead may be applied in all array new-expressions, including those referencing the library function operator new[](std::size_t, void*) and other placement allocation functions. The amount of overhead may vary from one invocation of new to another. ]
作者: starzhestarzhe    時(shí)間: 2011-01-31 10:28
new 用于單個(gè)對(duì)象或?qū)嵗膭?chuàng)建,就是調(diào)用類的構(gòu)造函數(shù)。
   new []用于創(chuàng)建對(duì)象或?qū)嵗臄?shù)組實(shí)例,并且地址是連續(xù)的。(內(nèi)存分配的時(shí)候有可能不連續(xù),但地址鏈表是連續(xù)的。)
作者: liwangli1983    時(shí)間: 2011-01-31 12:53
感謝樓上兩位,但兩位說的還是new和new[]的區(qū)別而不是operator new和operator new[]的區(qū)別,按理說operator new和operator new[]都應(yīng)該是單純返回一個(gè)void *型指針,不應(yīng)該有什么區(qū)別吧.

從二樓帖的ISO標(biāo)準(zhǔn)來看,new[]在調(diào)用operator new[]會(huì)傳遞一個(gè)比需要對(duì)象大的參數(shù),而且這個(gè)增加量要求是對(duì)象中最大對(duì)齊要求的整數(shù)倍,這樣在對(duì)operator new[]返回的已對(duì)齊指針進(jìn)行offset后,仍然能夠滿足對(duì)齊要求,符合通常的語義.這個(gè)增加量應(yīng)該是給delete []用來識(shí)別對(duì)象大小和數(shù)量以便多次析構(gòu)對(duì)象用的.

但這些活都是new[]做的,operator new[]只是單純的按要求返回了堆中內(nèi)存的指針,這個(gè)動(dòng)作和operator new沒什么區(qū)別吧?
作者: liwangli1983    時(shí)間: 2011-01-31 12:55
ISO C++03 5.3.4
10 A new-expression passes the amount of space requested to the allocation function ...
幻の上帝 發(fā)表于 2011-01-31 04:26



    請(qǐng)教一下ISO標(biāo)準(zhǔn)哪里去找,謝謝!
作者: liwangli1983    時(shí)間: 2011-01-31 13:53
本帖最后由 liwangli1983 于 2011-01-31 13:54 編輯

  1. #include<iostream>
  2. #include<cstdlib>
  3. using std::cout;
  4. using std::endl;
  5. using std::hex;
  6. class X
  7. {
  8.         public:
  9.                 X(int q = 0): a(q) {}
  10.                 ~X() { cout << "del" << endl; }
  11.         private:
  12.                 int a;
  13. };

  14. void *operator new[](size_t size)
  15. {
  16.         void *p = malloc(size);
  17.         cout << "new[]:" << size << endl;
  18.         cout << "new[]:" << hex << p << endl;
  19.         return p;
  20. }

  21. int main(void)
  22. {
  23.         X *q = new X[5];
  24.         cout << hex << q << endl;
  25.         cout << *(reinterpret_cast<int *>(q) - 1) << endl;
  26.         delete [] q;
  27.         q = new X(3);
  28.         cout << hex << q << endl;
  29.         delete q;

  30.         return 0;
  31. }
復(fù)制代碼
輸出結(jié)果是:
  1. abc@dlstd:~/cpptest/test_new$ ./a.out
  2. new[]:24
  3. new[]:0x804a008
  4. 0x804a00c
  5. 5
  6. del
  7. del
  8. del
  9. del
  10. del
  11. 0x804a028
  12. del
復(fù)制代碼
從測(cè)試程序來看,當(dāng)需要調(diào)用析構(gòu)函數(shù)的時(shí)候(之前實(shí)驗(yàn)new一個(gè)int數(shù)組,結(jié)果傳遞給operator new[]的size整好是所需要的20),傳遞給operator new[]的參數(shù)值是24,而實(shí)際上只需要20.從malloc返回的地址和new返回的地址來看,正好差了4,再?gòu)膔einterpret_cast后打印的數(shù)值來看,多占用的四字節(jié)是以int型保存的數(shù)值5,正好是數(shù)組的元素個(gè)數(shù).
從后一個(gè)new單個(gè)對(duì)象來看,重載operator new[]沒有影響到operator new.

還是不明白特意區(qū)分兩者的原因.
作者: w_anthony    時(shí)間: 2011-01-31 14:42
本帖最后由 w_anthony 于 2011-01-31 14:45 編輯
輸出結(jié)果是:從測(cè)試程序來看,當(dāng)需要調(diào)用析構(gòu)函數(shù)的時(shí)候(之前實(shí)驗(yàn)new一個(gè)int數(shù)組,結(jié)果傳遞給operator new[]的 ...
liwangli1983 發(fā)表于 2011-01-31 13:53



    operator new[]返回的地址偏移4個(gè)字節(jié),對(duì)于直接或者間接存在析構(gòu)函數(shù)的類才有意義,這樣在delete[]時(shí)刻程序才知道要按順序調(diào)用幾次析構(gòu)函數(shù),如果這個(gè)類沒有析構(gòu)函數(shù),那么部分編譯器甚至不會(huì)做這4個(gè)字節(jié)的偏移,這種情況下delete[]和delete是相同的(不過當(dāng)然也不能依賴編譯器的這個(gè)行為,當(dāng)用delete[]還是得用它)。
作者: liwangli1983    時(shí)間: 2011-01-31 15:20
operator new[]返回的地址偏移4個(gè)字節(jié),對(duì)于直接或者間接存在析構(gòu)函數(shù)的類才有意義,這樣在delet ...
w_anthony 發(fā)表于 2011-01-31 14:42



    多還是少那幾個(gè)字節(jié),完全是由new[]決定的.operator new[]僅僅是按照new[]傳過來的值分配相應(yīng)大小的內(nèi)存而已,從行為上來看operator new[]和operator new根本沒有區(qū)別啊.當(dāng)然如果用戶重載后就另當(dāng)別論了.
作者: w_anthony    時(shí)間: 2011-01-31 15:53
多還是少那幾個(gè)字節(jié),完全是由new[]決定的.operator new[]僅僅是按照new[]傳過來的值分配相應(yīng)大小 ...
liwangli1983 發(fā)表于 2011-01-31 15:20



    呃,你只是想問為什么重載operator new[]和operator new不設(shè)計(jì)成一個(gè)東西嗎?確實(shí)沒什么人只去重載其中的一個(gè),留下另外一個(gè)不去重載;蛟S只是當(dāng)初剛好設(shè)計(jì)這樣了吧。
作者: liwangli1983    時(shí)間: 2011-01-31 16:53
呃,你只是想問為什么重載operator new[]和operator new不設(shè)計(jì)成一個(gè)東西嗎?確實(shí)沒什么人只去重 ...
w_anthony 發(fā)表于 2011-01-31 15:53



    這么說來兩者行為其實(shí)沒有區(qū)別,只是由不的new操作符調(diào)用而已?
作者: liwangli1983    時(shí)間: 2011-01-31 19:34
編譯器在當(dāng)前模塊中同時(shí)加入global operator new & global operator new[];C++規(guī)范就這么規(guī)定的。
至于為 ...
掃地大叔 發(fā)表于 2011-01-31 19:16



    原來如此,謝謝
作者: OwnWaterloo    時(shí)間: 2011-02-01 06:25
我也很困惑……

似乎意圖是提供更小粒度的定制: 可讓new single object與 new array 使用不同的分配策略。
但因?yàn)檎w接口設(shè)計(jì)得不好, 單獨(dú)控制 new array并不好使……



為什么需要單獨(dú)的分配array的接口? 它與single object的不同?
我能想到的:

1. 可以獲得單個(gè)element 的大小, 而不是整個(gè)array的大小 —— 如同calloc那樣。

舉個(gè)例: 分配長(zhǎng)度為4的char數(shù)組 vs 長(zhǎng)度為1的int數(shù)組(假設(shè)sizeof(int)=4)
calloc 是能體現(xiàn)這種區(qū)別的: calloc(4, 1) vs calloc(1, 4)
而malloc就不能, 總是 malloc(4)

同時(shí), C/C++的分配函數(shù)都沒有alignment參數(shù), 那alignment要么是一個(gè)固定值, 要么是根據(jù)size來計(jì)算。
如果是根據(jù)size來計(jì)算, 并且使用取size中的所有2因子的做法。
那alignment_of(1) = 1, alignment_of(4) = 4, 前者的限制更小, 滿足需求的內(nèi)存更多。


但operator new[] 沒有類似calloc那樣的區(qū)分, 而是類似malloc的整體size……


2. array可能需要擴(kuò)展長(zhǎng)度, 而single object不必
那么, single object可能會(huì)來固定大小對(duì)象的池。
而array 可能會(huì)來自可擴(kuò)展的內(nèi)存(尾部被保留), 或者干脆多分配一些; 以避免擴(kuò)展時(shí)不夠, 移動(dòng)到新區(qū)域的開銷。

但是呢…… new/delete的機(jī)制并沒有在原地?cái)U(kuò)展的接口……
所以也用不上
作者: liwangli1983    時(shí)間: 2011-02-01 08:25
我也很困惑……

似乎意圖是提供更小粒度的定制: 可讓new single object與 new array 使用不同的分配策略 ...
OwnWaterloo 發(fā)表于 2011-02-01 06:25



    "似乎意圖是提供更小粒度的定制: 可讓new single object與 new array 使用不同的分配策略。"

應(yīng)該是這個(gè)原因吧
作者: 幻の上帝    時(shí)間: 2011-02-01 22:49
回復(fù) 5# liwangli1983

http://en.wikipedia.org/wiki/Typeid的Reference里面找到的:www-d0.fnal.gov/~dladams/cxx_standard.pdf。

那啥,問題看錯(cuò)了orz...

想了會(huì)兒覺得應(yīng)該也只有這個(gè)原因了——“似乎意圖是提供更小粒度的定制: 可讓new single object與 new array 使用不同的分配策略”。

Google了一下:
http://www.scs.stanford.edu/~dm/home/papers/c++-new.html

Why are operator new and operator new[] different? One often wants to implement a simple new/delete for a particular class (for instance by keeping a freelist) while not worrying about cases where one must allocate different-sized chunks of memory. Defining operators new and delete for a class but not new[] and delete[] accomplishes exactly this.

看來確實(shí)是這樣。
作者: liwangli1983    時(shí)間: 2011-02-02 08:35
回復(fù)  liwangli1983

的Reference里面找到的:www-d0.fnal.gov/~dladams/cxx_standard.pdf。

那啥,問 ...
幻の上帝 發(fā)表于 2011-02-01 22:49



    謝謝!




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2