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