C + + new restricted area allocation memory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.