これは、Windows 7 の MSVC++ 2010 で行った簡単なテストです。
// A struct with sizeof(s) == 4, e.g 4 bytes
struct s
{
int x;
};
// Allocate 1 million structs
s* test1 = new s[1000000];
// Memory usage show that the increase in memory is roughly 4 bytes * 1000000 - As expected
// NOW! If I run this:
for (int i = 0; i < 1000000; i++)
new s();
// The memory usage is disproportionately large. When divided by 1000000, indicates 64 bytes per s!!!
これは一般的な知識ですか、それとも何か不足していますか? 以前は、必要に応じてその場でオブジェクトを作成していました。たとえば、メッシュ内のすべての三角形に対して new Triangle() など。
個々のインスタンスの動的メモリ割り当てには、実際に桁違いのオーバーヘッドがありますか?
乾杯
編集:
g++ を使用して、Windows XP で動作中の同じプログラムをコンパイルして実行しました。オーバーヘッドは、以前に見られた 64 バイトではなく、16 バイトになりました。とても興味深い。