C/C++中的动态内存分配是指程序员手动执行内存分配。动态分配的内存在堆上分配,非静态和局部变量在堆栈上分配内存。
有什么应用?
- 动态分配内存的一种用途是分配可变大小的内存,而可变长度数组除外,这是编译器分配的内存无法实现的。
- 最重要的用途是为程序员提供了灵活性。无论何时需要和不再需要,我们都可以自由分配和取消分配内存。在许多情况下,这种灵活性会有所帮助。此类情况的示例是“链表”,“树”等。
它与分配给普通变量的内存有何不同?
对于“ int a”,“ char str [10]”等常规变量,将自动分配和释放内存。对于动态分配的内存,例如“ int * p = new int [10]”,程序员有责任在不再需要时释放内存。如果程序员不释放内存,则会导致内存泄漏(在程序终止之前不会释放内存)。
如何在C++中分配/取消分配内存?
C使用malloc()和calloc()函数在运行时动态分配内存,并使用free()函数释放动态分配的内存。C++支持这些功能,并且还有两个new和delete运算符,它们以更好和更轻松的方式执行分配和释放内存的任务。
new运算符
new运算符表示在堆上分配内存的请求。如果有足够的内存可用,则new运算符将初始化该内存,并将新分配和初始化的内存的地址返回给指针变量。
1. 使用new运算符的语法:要分配任何数据类型的内存,语法为:
pointer-variable = new data-type;
这里,pointer-variable是数据类型的指针。数据类型可以是任何内置数据类型,包括数组,也可以是任何用户定义的数据类型,包括结构和类。
示例:
// Pointer initialized with NULL
// Then request memory for the variable
int *p = NULL;
p = new int;
OR
// Combine declaration of pointer
// and their assignment
int *p = new int;
2. 初始化内存:我们还可以使用new运算符来初始化内存:
pointer-variable = new data-type(value);
示例:
int *p = new int(25);
float *q = new float(75.25);
3. 分配内存块:new运算符还用于分配data-type类型的内存块(数组)。
pointer-variable = new data-type[size];
其中size(一个变量)指定数组中元素的数量。
示例:
int *p = new int[10];
为int类型的10个整数连续动态分配内存,并将指针返回到序列的第一个元素,该元素分配给p(一个指针)。 p [0]表示第一个元素,p [1]表示第二个元素,依此类推。
4. 普通数组声明与使用new
声明普通数组与使用new分配内存块之间有区别。最重要的区别是,常规数组由编译器释放(如果数组是本地数组,则在函数返回或完成时释放)。但是,动态分配的数组始终保留在那里,直到程序员将其释放或程序终止为止。
5. 如果运行时没有足够的内存怎么办?
如果堆中没有足够的内存来分配,新请求将抛出std :: bad_alloc类型的异常,以指示失败,除非new操作符使用了“ nothrow”,在这种情况下它将返回NULL指针。因此,最好在使用new程序之前检查new产生的指针变量。
int *p = new(nothrow) int;
if (!p)
{
cout << "Memory allocation failed\n";
}
delete运算符
由于程序员负责分配动态分配的内存,因此C++语言为程序员提供了delete运算符。
语法:
// Release memory pointed by pointer-variable
delete pointer-variable;
这里,pointer-variable是指向new创建的数据对象的指针。
示例:
delete p;
delete q;
要释放指针变量指向的动态分配数组,请使用以下删除形式:
// Release block of memory
// pointed by pointer-variable
delete[] pointer-variable;
示例:
// It will free the entire array
// pointed by p.
delete[] p;
// C++ program to illustrate dynamic allocation
// and deallocation of memory using new and delete
#include <iostream>
using namespace std;
int main ()
{
// Pointer initialization to null
int* p = NULL;
// Request memory for the variable
// using new operator
p = new(nothrow) int;
if (!p)
cout << "allocation of memory failed\n";
else
{
// Store value at allocated address
*p = 29;
cout << "Value of p: " << *p << endl;
}
// Request block of memory
// using new operator
float *r = new float(75.25);
cout << "Value of r: " << *r << endl;
// Request block of memory of size n
int n = 5;
int *q = new(nothrow) int[n];
if (!q)
cout << "allocation of memory failed\n";
else
{
for (int i = 0; i < n; i++)
q[i] = i+1;
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << q[i] << " ";
}
// freed the allocated memory
delete p;
delete r;
// freed the block of allocated memory
delete[] q;
return 0;
}
输出:
Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5