0
我用下面的代碼重載了新的運算符。現在具有析構函數和新運算符的類
void* operator new(size_t size)
{
cout<<"size=>"<<size<<endl;
return malloc(size);
}
,我試圖使用該重載new
一個類對象分配內存。 說的那樣類體的定義:
class c
{
char ch;
};
我寫了下面的語句
c * p=new c; // gives me the output size=>1 OK Fine..
c *p=new c[100]; // gives me the output size => 100 OK fine.
,但現在我添加一個析構函數的類。 因此,新的身體變得:
class c
{
char ch;
public:
~c(){}
};
現在我又寫了相同的語句
c *p= new c;// gives me the output size=>1 OK Fine..
c *p=new c[100]; // gives me the output size => 108.
從何這額外的8而來?
我試圖用同樣的語句中重新分配陣列,並再次我有大小108(我有一個64位的操作系統,所以我想一個額外的指針被每次分配)。
爲什麼我的編譯器分配這個額外的指針,或者如果它的一些其他東西),當我們在類析構函數?
另請注意,你重載了'operator new'不正確。它應該在分配錯誤的情況下拋出'std :: bad_alloc',而不是返回0.它應該是'operator new []'數組。 – Lol4t0 2013-04-27 19:11:36