C ++ collection -- locate the new expression and the new expression

Source: Internet
Author: User

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
  • C ++ directory
Directory of all contents
  • CCPP Blog directory






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.