Summary of C + + review essentials four new and delete use

Source: Internet
Author: User

Dynamic establishment and release of objects

1 new and delete basic syntax

1) in the software development process, it is often necessary to dynamically allocate and revoke memory space, such as the insertion and deletion of nodes in a dynamic list. In the C language, the library functions malloc and free are used to allocate and revoke memory space. C + + provides simpler and more powerful operator new and delete to replace the malloc and free functions.

Note: New and delete are operators, not functions, so the execution is efficient.

2) Although C + + retains the malloc and free functions for compatibility with C, it is recommended that users do not use the malloc and free functions, but with the new and delete operators. Example of the new operator:

new int; Creates a storage space that holds integers and returns an address (that is, a pointer) to that storage space, new int (100); Opens a space that holds an integer and specifies that the integer has an initial value of 100 and returns a point to the storage space, new CHAR[10]; Open a space that holds the character array (including 10 elements) and returns the address of the first element, new int[5][4]; Open a space that holds a two-dimensional integer array (size 5*4), returning the address of the first element, float *p=new float (3.14159); Open a space for storing single-precision numbers, and specify the initial value of the real number as//3.14159, assigning the address of the returned space to the pointer variable p

You cannot specify an initial value when allocating array space with new. If space cannot be allocated properly due to insufficient memory, then new returns a null pointer null, which allows the user to determine whether the allocated space was successful based on the value of the pointer.

Application examples

Dynamic establishment and release of Class 2 objects

The

object defined with the class name is static, and the space occupied by the object cannot be released at any time during the program's run. But sometimes people want to set up objects when they need them, revoke them when they are not needed, and free up the memory space it occupies for other data to use. This can increase the utilization of the memory space.

C + +, you can use the new operator to dynamically establish an object, and use the delete operator to undo the object

For example:

Box *pt;//define a pointer variable to the Box class object pt

Pt=new Box; The starting address of the new object is stored in PT

The new object can be accessed through PT in the program. such as

cout<<pt->height;//Output The height member of the object

Cout<<pt->volume ();//Call the volume function of the object, Compute and output Volume

C + + also allows the newly created object to be initialized when new is executed. such as

box *pt=new box (12,15,18);
The
is used to combine the above two statements (defining pointer variables and creating new objects using new) into a single statement and specifying the initial value. This is more refined. The height,width and length in the

new object are given the initial value 12,15,18 respectively. The calling object can be either through the object name or through a pointer.

When performing a new operation, if the amount of memory is insufficient to open up the required memory space, most of the C + + compiler systems now return a 0 pointer value to new. Whenever a return value of 0 is detected, it is possible to determine if the allocated memory is successful. The

ANSI C + + standard proposes that, in the event of a failure to perform new, "throws" an "exception" that the user can handle based on the exception. However, the C + + standard still allows 0 pointer values to be returned in the event of a new failure. At present, different compiling systems have different methods to deal with new faults.

can be freed with the delete operator when it is no longer necessary to use an object created by new. such as

Delete pt;//Release The memory space PT points to

This revokes the object that PT points to. The object can no longer be used by the program thereafter.

If you use a pointer variable PT to point to a different dynamic object, be aware that the pointer variable is pointing at the current point, lest you delete the wrong object. When you execute the delete operator, the destructor is automatically called before the memory space is freed, completing the cleanup work.

3 in-depth analysis of new and malloc

Mixing test, comparison of similarities and differences

Conclusion: malloc does not invoke the constructor of a class

Free does not call the class's destructor


The above is the C + + review points summary of the four new and delete use of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.