C + + new operator

Source: Internet
Author: User

New data type assigned: Built-in data type, custom data type.

If unsuccessful, new returns 0 or throws an exception, and you can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function (with your function name as its parameter).

When new allocates memory for a C + + class object, the object's constructor is called after the memory is allocated. The new operator creates an array of objects only if the class has a default constructor.

When assigning multidimensional arrays, all dimensions except the first dimension must be constant expressions that evaluate to positive values, and the leftmost array dimension can be any expression that evaluates to a positive value. When the new operator allocates an array, the first dimension can be zero, at which point the new operator returns a unique pointer.

Example: Assigning a two-dimensional array, then releasing the size of the array to zhm x 10

1, char (*P) [Ten] = new CHAR[ZHM][10]; Delete[] p;

2, int m=2,n=3; int **p = new Int*[m];  for (int i=0;i<m;i++) p[i] = new Int[n]; ... for (int i=0;i<m;i++) delete[] p[i]; Delete[] p;

3, if 2 write: int **p;  P =new int* [m]; for (int i=0;i<m;i++) p[i] = new Int[n]; Do not write: (*p) =new int [m];

A variable cannot contain a const, a volatile class declaration, or an enumeration declaration. Illegal: volatile char * p = new volatile char[10];

The new operator does not assign reference types because these types are not objects.

The new operator cannot be used to assign functions, but it can be used to assign pointers to functions. The following example assigns an array of 7 pointers to a function that returns an integer, and then releases it.

Int (**p) () = new (int (*[7)) ()); Delete *p;

    

C + + new operator

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.