Allocating memory from a specified region in C + +:
#include <iostream>using namespace Std;const int buf (1024x768); char buffer[buf]={0}; The static area//p1,p3 as a pointer variable in the stack area, the stored address points to the heap area//Manually frees the memory//P2,P4 as a pointer variable in the stack area, the stored address points to the static zone//automatically frees the memory for allocation of data that is no longer used//to avoid memory leaks and automatically frees memory. At the expense of the memory access independence,//from the program output can be seen P2 and P4 the same address of void main () {int len=5;int *p1,*p2,*p3,*p4;p1=new Int[len];p 2= new (buffer) Int[len ];p 3=new Int[len];p 4= new (buffer) int[len];//with the specified area to allocate, starting from the buffer header address cout<< "Buffer:" <<&buffer< <endl;for (int i=0;i<len;i++) {* (p1+i) =i+1;* (p2+i) =i+2;cout<< "P1:" << (p1+i) << " " < <* (p1+i) << " P2:" << (p2+i) << "<<* (p2+i) <<endl;} cout<<endl<<endl;for (int i=0;i<len;i++) {* (p3+i) =i+1;* (p4+i) =i+2;cout<< "P3:" << (P3+i) << " <<* (p3+i) <<" P4: "<< (p4+i) <<" "<<* (P4+i) <<endl;} Delete P1;delete p3;cin.get ();}
C + + new restricted area allocation memory