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
C + + supplements--positioning the new expression