char str[1024] = { 0 };//全局静态区
void main1()
{
int *p1 = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//堆上创建 开辟的内存
int *p2 = new (str)int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//ok 在str字段处开始 分配内存
int *p3 = new (str+40)int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//ok str+40 在str字段处的后40字节数处开始分配
for (int i = 0; i < 10; i++)
{
cout << p1[i] << " " << p1 + i << " " << p2[i] << " " << p2 + i << endl;
}
cout << endl;
p1 = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//堆上创建 开辟的内存
p2 = new (str)int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//在str字段处开始 分配内存
for (int i = 0; i < 10; i++)
{
cout << p1[i] << " " << p1 + i << " " << p2[i] << " " << p2 + i << endl;
}
cin.get();
}
void main()
{
char str1[1024] = { 0 };//栈区
char *str = new char[1024]{0};//堆区
int *p1 = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//堆上创建 开辟的内存
int *p2 = new (str)int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//str+40 在str字段处的后40字节数处开始分配
for (int i = 0; i < 10; i++)
{
cout << p1[i] << " " << p1 + i << " " << p2[i] << " " << p2 + i << endl;
}
cout << endl << endl;
free(str);//释放堆上的数据
p1 = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//堆上创建 开辟的内存
p2 = new (str)int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};//在str字段处开始 分配内存
for (int i = 0; i < 10; i++)
{
cout << p1[i] << " " << p1 + i << " " << p2[i] << " " << p2 + i << endl;
}
cin.get();
}
更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。
关于公众号的描述访问如下链接