Home > Backend Development > C#.Net Tutorial > Summary of C++ Review Key Points Part 4: Use of New and delete

Summary of C++ Review Key Points Part 4: Use of New and delete

黄舟
Release: 2017-01-16 11:23:30
Original
1720 people have browsed it

Dynamic establishment and release of objects

1 Basic syntax of new and delete

1) In the process of software development, it is often necessary to dynamically allocate and cancel memory space, such as in dynamic linked lists. Insertion and deletion of nodes. In C language, the library functions malloc and free are used to allocate and deallocate memory space. C++ provides simpler and more powerful operators new and delete to replace the malloc and free functions.

Note: new and delete are operators, not functions, so their execution efficiency is high.

2) Although C++ still retains the malloc and free functions in order to be compatible with the C language, it is recommended that users use the new and delete operators instead of the malloc and free functions. Example of new operator:

new int; //开辟一个存放整数的存储空间,返回一个指向该存储空间的地址(即指针)
new int(100); //开辟一个存放整数的空间,并指定该整数的初值为100,返回一个指向该存储空间的地址
new char[10]; //开辟一个存放字符数组(包括10个元素)的空间,返回首元素的地址
new int[5][4]; //开辟一个存放二维整型数组(大小为5*4)的空间,返回首元素的地址
float *p=new float (3.14159); //开辟一个存放单精度数的空间,并指定该实数的初值为//3.14159,将返回的该空间的地址赋给指针变量p
Copy after login

The initial value cannot be specified when using new to allocate array space. If space cannot be allocated normally due to insufficient memory or other reasons, new will return a null pointer NULL, and the user can determine whether the space allocation is successful based on the value of the pointer.

Application examples

Summary of C++ Review Key Points Part 4: Use of New and delete

2 Dynamic creation and release of class objects

Objects defined using class names are static , during the running of the program, the space occupied by the object cannot be released at any time. But sometimes people want to create an object when it is needed, destroy it when it is no longer needed, and release the memory space it occupies for other data. This improves memory space utilization.

In C++, you can use the new operator to dynamically create an object and the delete operator to cancel the object.

For example:

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

pt=new Box; //The starting address of the new object is stored in pt

You can access this newly created object through pt in the program. Such as

coutheight; //Output the height member of the object

coutvolume( ); //Call the volume of the object Function, calculate and output volume

C++ also allows the newly created object to be initialized when executing new. For example,

Box *pt=new Box(12,15,18);

This way of writing is to merge the above two statements (defining pointer variables and using new to create new objects) into one statement and specify the initial value. This is more refined.

The height, width and length in the new object obtain initial values ​​​​12, 15, and 18 respectively. The object can be called either by object name or by pointer.

When executing the new operation, if the amount of memory is insufficient and the required memory space cannot be opened, most current C++ compilation systems will cause new to return a 0 pointer value. As long as you check whether the return value is 0, you can determine whether the memory allocation is successful.

ANSI C++ standard proposes that when a failure occurs during the execution of new, an "exception" will be "thrown", and the user can perform relevant processing based on the exception. But the C++ standard still allows a pointer value of 0 to be returned when a new fault occurs. Currently, different compilation systems handle new failures in different ways.

When the object created by new is no longer needed, you can use the delete operator to release it. Such as

delete pt; //Release the memory space pointed to by pt

This cancels the object pointed to by pt. The object cannot be used by the program thereafter.

If you use a pointer variable pt to point to different dynamic objects one after another, you should pay attention to the current pointer of the pointer variable to avoid deleting the wrong object. When the delete operator is executed, the destructor is automatically called to complete the cleanup work before releasing the memory space.

3 In-depth analysis of new and malloc

Mixed testing, comparison of similarities and differences

Conclusion: malloc will not call the constructor of the class

Free will not call the destructor of the class


The above is the content of the fourth summary of C++ review points New and delete. For more related content, please pay attention to the PHP Chinese website ( www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template