C++拾遗--定位new表达式
new表达式,默认下把内存开辟到堆区。使用定位new表达式,可以在指定地址区域(栈区、堆区、静态区)构造对象,这好比是把内存开辟到指定区域。
#include <iostream> using namespace std; char addr1[100]; int main() { cout << "******定位new表达式演示***by David***" << endl; char addr2[100]; char *addr3 = new char[100]; cout << "addr1 = " << (void*)addr1 << endl; cout << "addr2 = " << (void*)addr2 << endl; cout << "addr3 = " << (void*)addr3 << endl; int *p = nullptr; //把内存分配到静态区 p = new(addr1)int; *p = 1; cout << (void*)p << " " << *p << endl; //把内存分配到栈区 p = new(addr2)int; *p = 2; cout << (void*)p << " " << *p << endl; //把内存分配到堆区 p = new(addr3)int; *p = 3; cout << (void*)p << " " << *p << endl; cin.get(); return 0; }运行
原文:http://blog.csdn.net/zhangxiangdavaid/article/details/43899351