[问题] overload new []

楼主: nickyhsu (火星牧羊人)   2012-11-18 13:39:44
讲义 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)
作者: yuting1105 (yuting)   2011-01-18 14:50:00
老师上课时有说class若不加destructor会有这种情况看来不用class时一般的variable也是一样的情况
作者: djshen (djshen)   2011-01-18 14:51:00
built-in type
作者: yuting1105 (yuting)   2011-01-18 14:51:00
跟complier的版本应该没有关系
作者: stmharry (橘子)   2011-01-18 15:32:00
实测结果也是跟cstr与destr有没有explicitly宣告有关
作者: ric2k1 (Ric)   2011-01-18 17:12:00
恩恩,一楼上课有在认真听哦!

Links booklink

Contact Us: admin [ a t ] ucptt.com