C++ Memory Management Library
C++ is a powerful programming language that provides a rich standard library to help developers write code more efficiently.
In C++, is a very important header file that contains functions and exception types for dynamic memory allocation.
Dynamic memory allocation allows a program to request memory at runtime, which is very useful when dealing with data structures of uncertain size.
The header defines the following key components:
newoperator: used to dynamically allocate memory.deleteoperator: used to free dynamically allocated memory.nothrowoperator: used to not throw an exception when memory allocation fails.std::bad_allocexception: thrown when memory allocation fails.
Syntax
Use new operator
new operator is used to allocate memory on the heap. Its basic syntax is as follows:
pointer new (type [, initializer]);
pointeris a pointer to the allocated memory.typeis the type of the object to allocate.initializeris an optional initialization expression.
Use delete operator
delete operator is used to free memory previously allocated with new . Its basic syntax is as follows:
delete pointer;
pointeris the pointer to memory previously allocated withnew.
Example
Dynamically allocate a single object:
Example
#include
#include
class
MyClass
{
public
:
int
value
;
MyClass
(
)
:
value
(
0
)
{
}
}
;
int
main
(
)
{
MyClass
myObject
=
new
MyClass
;
// Allocate a MyClass object
myObject
value
=
10
;
// Access members using the dot operator
std
::
cout
<<
"Value: "
<<
myObject
value
<<
std
::
endl
;
delete
myObject
;
// Free memory
return
0
;
}
Output result:
Value: 10
Dynamically allocate an array:
Example
#include
#include
int
main
(
)
{
int
myArray
=
new
int
[
10
]
;
// Allocate an array containing 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
Use nothrow to avoid exceptions:
Example
#include
#include
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
;
// Free 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
#include
int
main
(
)
{
try
{
int
myArray
=
new
int
[
10000000
]
;
// Try to allocate a large array
std
::
cout
<<
"Memory allocation succeeded."
<<
std
::
endl
;
delete
[
]
myArray
;
// Free memory
}
catch
(
const
std
::
bad_alloc
&
e
)
{
std
::
cout
<<
"Exception caught: "
<<
e.
what
(
)
<<
std
::
endl
;
}
return
0
;
}
Output result:
Exception caught: std::bad_alloc // 如果内存分配失败