C ++ collection -- locate the new expression and the new expression
C ++ collection-locate the new expression
Preface
New expression. By default, the memory is opened to the heap area. By using the new expression, you can construct an object in the specified address area (stack area, heap area, static area), which is like opening the memory to the specified area.
Common forms of new expressions for positioning in the body
- New (address) type;
- New (address) type (initializers );
- New (address) type [size];
- New (address) type [size] {braced initializer list };
Locate the new expression and call void * operator new (size_t, void *) to allocate memory. Sample Code
# Include <iostream> using namespace std; char addr1 [100]; int main () {cout <"******* Positioning new expression demonstration ***** by David *****" <endl; char addr2 [100]; char * addr3 = new char [100]; cout <"addr1 =" <(void *) addr1 <endl; cout <"addr2 =" <(void *) addr2 <endl; cout <"addr3 =" <(void *) addr3 <endl; int * p = nullptr; // allocate the memory to the static zone p = new (addr1) int; * p = 1; cout <(void *) p <"" <* p <endl; // allocate the memory to the stack zone p = new (addr2) int; * p = 2; cout <(void *) p <"" <* p <endl; // allocate the memory to the heap area p = new (addr3) int; * p = 3; cout <(void *) p <"" <* p <endl; cin. get (); return 0 ;}
Run
Contents of this column
Directory of all contents