C++ Memory Management Library<new>
C++ is a powerful programming language that provides a rich standard library to help developers write code more efficiently.
In C++,<new>It is a very important header file that contains functions and exception types for dynamic memory allocation.
Dynamic memory allocation allows programs to request memory at runtime, which is very useful when dealing with data structures of uncertain size.
<new>The header file defines the following key components:
newOperator: used for dynamic memory allocation.deleteOperator: used to release dynamically allocated memory.nothrowOperator: used to not throw an exception when memory allocation fails.std::bad_allocException: thrown when memory allocation fails.
Syntax
UsagenewOperators
newOperator used to allocate memory on the heap. Its basic syntax is as follows:
<code class="language-cpp">pointer new (type [, initializer]);</code>
pointeris a pointer to the allocated memory.typeis the type of the object to be allocated.initializeris an optional initialization expression.
UsagedeleteOperators
deleteOperator is used to release previouslynewallocated memory. Its basic syntax is as follows:
<code class="language-cpp">delete pointer;</code>
pointeris previously usednewPointer to the allocated memory.
Example
Dynamically allocating a single object:
Example
#include <new> // include the <new> header file
class MyClass {
public:
int value;
MyClass() : value(0) {}
};
int main() {
MyClass* myObject = new MyClass; // Allocate a MyClass object
myObject->value = 10; // Use the dot operator to access members
std::cout << "Value: " << myObject->value << std::endl;
delete myObject; // release memory
return 0;
}
Output result:
Value: 10
Dynamically allocated array:
Example
#include <new>
int main() {
int* myArray = new int[10]; // Allocate an array of 10 integers
for (int i = 0; i < 10; ++i) {
myArray[i] = i * 2; // Initialize the array
}
for (int i = 0; i < 10; ++i) {
std::cout << "Array[" << i << "]: " << myArray[i] << std::endl;
}
delete[] myArray; // Free the array memory
return 0;
}
Output result:
Array[0]: 0 Array[1]: 2 Array[2]: 4 Array[3]: 6 Array[4]: 8 Array[5]: 10 Array[6]: 12 Array[7]: 14 Array[8]: 16 Array[9]: 18
Using nothrow to avoid exceptions:
Example
#include <new>
int main() {
int* myArray = new(std::nothrow) int[10000000]; // Try to allocate a large array
if (!myArray) {
std::cout << "Memory allocation failed." << std::endl;
} else {
std::cout << "Memory allocation succeeded." << std::endl;
delete[] myArray; // release memory
}
return 0;
}
Output result:
Memory allocation failed. // 或者 Memory allocation succeeded. 取决于系统内存情况
Exception Handling
When memory allocation with the new operator fails, C++ throws a std::bad_alloc exception.
Developers can catch and handle this exception using a try-catch block.
Example
#include <new>
int main() {
try {
int* myArray = new int[10000000]; // Try to allocate a large array
std::cout << "Memory allocation succeeded." << std::endl;
delete[] myArray; // release memory
} catch (const std::bad_alloc& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Output result:
Exception caught: std::bad_alloc // 如果内存分配失败other extensions