
new和malloc
- 
new从自由存储区分配内存空间 malloc从堆上分配内存空间 
- 
new返回对象类型指针 malloc返回void *, 需要强制转化 
- 
new内存分配失败抛出bac_alloc异常, 不会返回NULL malloc失败时返回NULL int *a = (int *)malloc (sizeof (int )); if(NULL == a) { ... } else { ... } try { int *a = new int(); } catch (bad_alloc) { ... }
- 
malloc需要显示指定类型大小 malloc(sizeof(int))
- 
构造/析构函数 - new操作符
- 调用operator new函数, 分配一块足够大的,原始的,未命名的内存空间以便存储特定类型的对象
- 构造对象, 传入初值
- 返回对象指针
 
- delete操作符
- 析构函数
- operator delete
 
 
- new操作符
- 
数组 - new: A * ptr = new A[10];
- malloc: int * ptr = (int *) malloc(sizeof(int)* 10);
 
- new: 
- 
operator new的实现可以基于malloc 
- 
opeartor new /operator delete可以被重载 
new和delete
智能指针
std::auto_ptr<report> ps(new report("using auto ptr"));
ps->comment();lambda
数据结构
map, set
11 & 14 & 17 新特性
11
- auto
奇怪的笔试题~
- 
sizeof(class) #include <iostream> // 占位符 class A { public: A() = default; }; // 一个虚表指针 class B { void f1() {} virtual void f2() {} }; // 一个虚表指针 class C : public B { }; // class D { int a; char *p; }; int main() { printf("%lu\n", sizeof(A)); printf("%lu\n", sizeof(B)); printf("%lu\n", sizeof(C)); printf("%lu\n", sizeof(D)); } 