[問題] overload new []

看板EE_DSnP作者 (火星牧羊人)時間11年前 (2012/11/18 13:39), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串1/1
講義 07. memory management p.29 參考連結 The only difference is that the size passed to new[] takes into account the total size of the array plus some additional data used by the compiler to distinguish between pointers to objects and arrays of objects. 傳進去的參數會多要 addition data 的空間 但是實驗結果好像沒有 === void* operator new[](size_t t) { cout << "size: " << t << endl; void *p = malloc(t); cout << "in: " << (unsigned)p << endl; return p; } int main() { int *p = new int[4]; cout << "out: " << (unsigned)p << endl; return 0; } === size: 16 in: 134520840 out: 134520840 16 = 4 (int) x 4 === http://www.parashift.com/c++-faq-lite/num-elems-in-new-array.html [16.14] After p = new Fred[n], how does the compiler know there are n objects to be destructed during delete[] p? 似乎 compiler 不一定會用 over-allocate 的方式 === 我用的 compiler 是 gcc version 4.3.2 (Debian 4.3.2-1.1) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.136.105.85

11/18 14:50, , 1F
老師上課時有說class若不加destructor會有這種情況
11/18 14:50, 1F

11/18 14:50, , 2F
看來不用class時一般的variable也是一樣的情況
11/18 14:50, 2F

11/18 14:51, , 3F
built-in type
11/18 14:51, 3F

11/18 14:51, , 4F
跟complier的版本應該沒有關係
11/18 14:51, 4F

11/18 15:32, , 5F
實測結果也是跟cstr與destr有沒有explicitly宣告有關
11/18 15:32, 5F

11/18 17:12, , 6F
恩恩,一樓上課有在認真聽哦!
11/18 17:12, 6F
文章代碼(AID): #1Gg7IZQh (EE_DSnP)