Detailed explanation of the usage of new in C++

hzc
Release: 2020-06-19 11:57:31
Original
12377 people have browsed it

Detailed explanation of the usage of new in C++

Detailed explanation of the usage of new in c

The new operator in C is used to dynamically allocate and deallocate memory.

1. Open up a single variable address space

new int; //Open up a storage space to store the array and return an address pointing to the storage space. int *a = new int means assigning an int type address to an integer pointer a. 2) int *a = new int(5) has the same effect as above, but at the same time assigns the integer value to 5.

2. Open up the array space

To access the structure space opened by new, you cannot directly access it through the variable name, you can only access it through the assigned pointer. Use new to dynamically open and cancel the address space. When programming, if you use up a variable and need to use it again next time, you can open up a space each time you start using it, and cancel it after use.

Detailed explanation of the usage of new in C++

Extended information:

Notes on using new in C:

1. The user cannot take the initiative To call the constructor, you need to use placement new, but the user can actively call the destructor, so after using these objects, call the destructor, and then use the corresponding method to allocate memory to release the memory.

2. In fact, malloc does not necessarily save much time than operator new. Placement new is often used to consider performance, so it is used together with the memory pool.

Recommended tutorial: "c "

The above is the detailed content of Detailed explanation of the usage of new in C++. For more information, please follow other related articles on the PHP Chinese website!

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