3. 下面我们来看看上面提到的4字节是干什么用的。
int main()
{
A *a = new A[100];
printf("Adress after new = %p\n", a);
printf("number of A at %p = %d\n", (int*)a-1, *((int*)a-1));
printf("size of a at %p = %d\n", (int*)a-2, *((int*)a-2));
delete[] a;
return 0;
}
运行结果:
Adress after malloc: 0x804a008; size: 104
A::A()
...
Adress after new = 0x804a00c
number of A at 0x804a008 = 100
size 阅读全文>
发表于 @ 2008年11月23日 11:02:00|评论(loading...)|收藏
1. 按C++标准的说法,对于非数组类型,分配函数是operator new,释放函数是operator delete。对于数组类型,分配函数operator new[],释放函数是operator delete[]。
If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete. If the allocated type is an array type, the allocation function’s name is operator new[] and the
deallocation function’s name is operator delete[].
new操作符的作用是先调用分配函数分配足够的内存以便容纳所需类型的对象,再调用构造函数初始化内存中的对象。 delelte操作符相阅读全文>
发表于 @ 2008年11月23日 11:01:00|评论(loading...)|收藏