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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
12下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 12975 | 回復(fù): 14
打印 上一主題 下一主題

關(guān)于operator new和operator new[]的區(qū)別 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-01-30 21:25 |只看該作者 |倒序?yàn)g覽
如題,new和new[]的區(qū)別比較明顯了,主要是new[]會(huì)多次調(diào)用構(gòu)造函數(shù),依次初始化對(duì)象。

但operator new和operator new[]不都是單純的返回一個(gè)指向heap的void*指針么?有什么區(qū)別呢?請(qǐng)大家指教,謝謝!

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 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. ]

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2011-01-31 10:28 |只看該作者
new 用于單個(gè)對(duì)象或?qū)嵗膭?chuàng)建,就是調(diào)用類(lèi)的構(gòu)造函數(shù)。
   new []用于創(chuàng)建對(duì)象或?qū)嵗臄?shù)組實(shí)例,并且地址是連續(xù)的。(內(nèi)存分配的時(shí)候有可能不連續(xù),但地址鏈表是連續(xù)的。)

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2011-01-31 12:53 |只看該作者
感謝樓上兩位,但兩位說(shuō)的還是new和new[]的區(qū)別而不是operator new和operator new[]的區(qū)別,按理說(shuō)operator new和operator new[]都應(yīng)該是單純返回一個(gè)void *型指針,不應(yīng)該有什么區(qū)別吧.

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

但這些活都是new[]做的,operator new[]只是單純的按要求返回了堆中內(nèi)存的指針,這個(gè)動(dòng)作和operator new沒(méi)什么區(qū)別吧?

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 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)哪里去找,謝謝!

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 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è)試程序來(lái)看,當(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返回的地址來(lái)看,正好差了4,再?gòu)膔einterpret_cast后打印的數(shù)值來(lái)看,多占用的四字節(jié)是以int型保存的數(shù)值5,正好是數(shù)組的元素個(gè)數(shù).
從后一個(gè)new單個(gè)對(duì)象來(lái)看,重載operator new[]沒(méi)有影響到operator new.

還是不明白特意區(qū)分兩者的原因.

論壇徽章:
9
摩羯座
日期:2013-08-15 15:18:48獅子座
日期:2013-09-12 18:07:47金牛座
日期:2013-09-16 13:23:09辰龍
日期:2013-10-09 09:03:27白羊座
日期:2013-10-17 13:32:44子鼠
日期:2014-04-23 15:09:38戌狗
日期:2014-09-17 11:37:542015年亞洲杯之韓國(guó)
日期:2015-03-26 10:16:442015亞冠之武里南聯(lián)
日期:2015-08-18 14:55:52
7 [報(bào)告]
發(fā)表于 2011-01-31 14:42 |只看該作者
本帖最后由 w_anthony 于 2011-01-31 14:45 編輯
輸出結(jié)果是:從測(cè)試程序來(lái)看,當(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ù)的類(lèi)才有意義,這樣在delete[]時(shí)刻程序才知道要按順序調(diào)用幾次析構(gòu)函數(shù),如果這個(gè)類(lèi)沒(méi)有析構(gòu)函數(shù),那么部分編譯器甚至不會(huì)做這4個(gè)字節(jié)的偏移,這種情況下delete[]和delete是相同的(不過(guò)當(dāng)然也不能依賴(lài)編譯器的這個(gè)行為,當(dāng)用delete[]還是得用它)。

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2011-01-31 15:20 |只看該作者
operator new[]返回的地址偏移4個(gè)字節(jié),對(duì)于直接或者間接存在析構(gòu)函數(shù)的類(lèi)才有意義,這樣在delet ...
w_anthony 發(fā)表于 2011-01-31 14:42



    多還是少那幾個(gè)字節(jié),完全是由new[]決定的.operator new[]僅僅是按照new[]傳過(guò)來(lái)的值分配相應(yīng)大小的內(nèi)存而已,從行為上來(lái)看operator new[]和operator new根本沒(méi)有區(qū)別啊.當(dāng)然如果用戶(hù)重載后就另當(dāng)別論了.

論壇徽章:
9
摩羯座
日期:2013-08-15 15:18:48獅子座
日期:2013-09-12 18:07:47金牛座
日期:2013-09-16 13:23:09辰龍
日期:2013-10-09 09:03:27白羊座
日期:2013-10-17 13:32:44子鼠
日期:2014-04-23 15:09:38戌狗
日期:2014-09-17 11:37:542015年亞洲杯之韓國(guó)
日期:2015-03-26 10:16:442015亞冠之武里南聯(lián)
日期:2015-08-18 14:55:52
9 [報(bào)告]
發(fā)表于 2011-01-31 15:53 |只看該作者
多還是少那幾個(gè)字節(jié),完全是由new[]決定的.operator new[]僅僅是按照new[]傳過(guò)來(lái)的值分配相應(yīng)大小 ...
liwangli1983 發(fā)表于 2011-01-31 15:20



    呃,你只是想問(wèn)為什么重載operator new[]和operator new不設(shè)計(jì)成一個(gè)東西嗎?確實(shí)沒(méi)什么人只去重載其中的一個(gè),留下另外一個(gè)不去重載;蛟S只是當(dāng)初剛好設(shè)計(jì)這樣了吧。

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2011-01-31 16:53 |只看該作者
呃,你只是想問(wèn)為什么重載operator new[]和operator new不設(shè)計(jì)成一個(gè)東西嗎?確實(shí)沒(méi)什么人只去重 ...
w_anthony 發(fā)表于 2011-01-31 15:53



    這么說(shuō)來(lái)兩者行為其實(shí)沒(méi)有區(qū)別,只是由不的new操作符調(diào)用而已?
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP