C + + supplements--positioning the new expression

Source: Internet
Author: User

C + + supplements--positioning the new expression

Preface

The new expression, by default, opens up the memory to the heap area. Using the positional new expression, you can construct an object in the specified address area (stack, heap, static), which is akin to opening the memory to a specified area.

Bodylocating the common form of the new expression

    • New (address) type;
    • New (address) type (initializers);
    • New (address) type[size];
    • New (address) type[size]{braced initializer list};
The position new expression calls void *operator new (size_t, void *), and allocates memory. Sample Code
#include <iostream>using namespace Std;char addr1[100];int main () {cout << "****** positioning new expression demo ***by david***" << Endl;char Addr2[100];char *ADDR3 = new Char[100];cout << "ADDR1 =" << (void*) addr1 << Endl;cou  T << "ADDR2 =" << (void*) addr2 << endl;cout << "ADDR3 =" << (void*) ADDR3 << endl;int *p = nullptr;//allocates memory to static zone P = new (ADDR1) int;*p = 1;cout << (void*) p << "  " << *p << endl;//to divide memory allocated to stack P = new (ADDR2) int;*p = 2;cout << (void*) p << "  " << *p << endl;//allocate memory to heap area P = new (ADDR3) int ; *p = 3;cout << (void*) p << "  " << *p << endl;cin.get (); return 0;}
Run










directory of this column
    • C + + Supplements directory
Directory of all content
    • CCPP Blog Directory






C + + supplements--positioning the new expression

Related Article

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.