main.cpp
#include <stdlib.h>
#include <iostream>
#include <new>
static size_t newCount = 0;
void *operator new(size_t size)
{
++newCount;
std::cout << "(" << newCount << ") " << "new " << size << "bytes" << std::endl;
return malloc(size);
}
void operator delete(void *p)
{
--newCount;
std::cout << "(" << newCount << ") " << "delete " << p << std::endl;
free(p);
}
class Hoge
{
int a, b;
char c;
friend std::ostream &operator<<(std::ostream &os, const Hoge &h)
{
return os << h.a << " " << h.b << " " << h.c;
}
public:
Hoge():a(), b(100),c('a'){}
};
int main(int argc, char *argv[])
{
Hoge *ar = new Hoge [10];
for(int i=0; i<10; ++i)
{
std::cout << ar[i] << std::endl;
}
delete[] ar;
return 0;
}
出力(1) new 120bytes 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a 0 100 a (0) delete 0x804a008int2個、char1個のインスタンス10個でなぜ120バイト確保されるかわからなければ、パディングで調べてみるといいと思います。
参考URL
0 件のコメント:
コメントを投稿