2016-08-10
- 以下的代码编译器是支持的
1 | int func(int * ){ |
- set_new_handler(new_handler) function,当使用new开辟内存空间失败后,会调用new_handler函数 http://en.cppreference.com/w/cpp/memory/new/set_new_handler
- operation new http://en.cppreference.com/w/cpp/memory/new/operator_new
2016-08-11
- 注意以下例子, ::a表示的是全局变量a。
1 |
|
2016-08-15
new (int (*[10])()); // okay: allocates an array of 10 pointers to functions
new int(*[10])(); // error: parsed as (new int) (*[10]) ()
- 关于函数指针,其关键在于把*写在了括号里面。看下面的例子
1 |
|
- 关于expression new:
(optional) new (placement_params)(optional) ( type ) initializer(optional)
或(optional) new (placement_params)(optional) type initializer(optional)
, 如果表达式中不包含placement_params的话,编译器会首先分配相应内存空间(可能会调用operation new来分配内存空间)。如果带placement_params的话,将不会再分配内存了。文档里是这么说的“If placement_params are provided, they are passed to the allocation function as additional arguments. Such allocation functions are known as “placement new”, after the standard allocation function void operator new(std::size_t, void), which simply returns its second argument unchanged. This is used to construct objects in allocated storage:”
1 | char* ptr = new char[sizeof(T)]; // allocate memory |