一. 使用new和delete运算符时PF率的变化情况
Ctrl+Alt+Del进入任务管理器、性能,运行下列代码,并观察PF率的变化。可知,new运算符增加PF率,delete使PF率还原。注意:使用 new 得来的空间,必须用 delete 来释放;使用 new [] 得来的空间,必须用 delete [] 来释放。彼此之间不能混用。用 new [] 分配出连续空间后,指针变量“指向”该空间的首地址。
#include<iostream.h>
#include<stdio.h>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
{
cout<<"按任意键开始分配内存
"<<endl;
getchar();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
unsigned char *p = new unsigned char[1024*1024*100];
cout<<"成功分配了100M的内存"<<endl;
getchar();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete []p;
cout<<"释放所分配的100M内存"<<endl;
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int arr[20]; //定义
arr[0] = 100; //让第一个元素为100
for (int i = 1; i < 20; i++)
{
arr[i] = arr[i-1] + 50;
}
for (int i = 0; i < 20; i++) //输出
{
cout << arr[i] << endl;
}
//通过+来得到指定元素,也可通过[]
cout << *(arr + 0) << endl; //*(arr+0) 等于 *arr
cout << *(arr + 1) << endl;
cout << *(arr + 1) << endl;
指针:
int *p = new int[20]; //定义
p[0] = 100; //让第一个元素为100
for (int i = 1; i < 20; i++)
{
p[i] = p[i-1] + 50;
}
for (int i = 0; i < 20; i++) //输出
{
cout << p[i] << endl;
}
//通过+来得到指定元素,也可通过[]
cout << *(p + 0) << endl; //*(p+0) 等于 *p
cout << *(p + 1) << endl;
cout << *(p + 1) << endl;
两者的不同之处:
数组:
//定义并初始化
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//不能通过对数组本身+或-来改变数组的位置
arr = arr + 1; //错!
cout << *arr << endl;
arr++; //错!
cout << *arr << endl;
arr--; //错!
cout << *arr << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//数组所带的空间由系统自动分配及回收,无须也无法由程序来直接释放
指针:
//定义并且生成空间,但不能直接初始空间的内容
int *p = new int[20] {0,1,2,3,4 ……}; // 错!
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//只得通过循环一个个设置
for (int i=0; i<20; i++)
{
p[i] = i;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//可以通过+或-操作直接改变指针
p = p + 1;
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
p++;
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
p--;
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//指向连续空间的指针,必须使用delete[]来释放
delete [] p;
三. delete/delete[]的几个注意点
1. 指针通过 new 或 new[] ,向系统“申请”得到一段内存空间,这段内存空间必须在不需要将它释放了。
int* p = new int[100];
int girl[100];
p = girl;
delete [] p;
灾难在 delete [] p 时发生。我们原意是要释放p最初通过new int[100]而得到的内存空间,但事实上,p那时已经指向girl[100]了。结果,第一、最初的空间并没有被释放。第二、girl[100] 本由系统自行释放,现在我们却要强行释放它。
2. 一个指针被删除时,应指向最初的地址
当一个指针通过 +,- 等操作而改变了指向;那么在释放之前,应确保其回到原来的指向。如下所示:在 delete [] p 时,p指向的是第二个元素,结果该释放将产生错位:第一个元素没有被释放,而在最后多删除了一个元素。
int *p = new int[3];
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
*p = 1;
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
p++; //p的指向改变了,指向了下一元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
*p = 2;
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete [] p; //错误的释放
如何消除这一严重错误呢?
第一种方法是把指针正确地"倒"回原始位置:
p--;
delete [] p;
但当我们的指针指向变化很多次时,在释放前要保证一步不错地一一退回,会比较困难。所以另一方法是在最初时“备份”一份。在释放时,直接释放该指针即可。
int* p = new int[3];
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int* pbak = *p; //备份
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//移动 p
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
……
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete [] pbak; //释放
由于pbak正是指向p最初分配后的地址,我们删除pbak,就是删除p最初的指向。此时我们不能再删除一次p。这也就引出new / delete 及 new[] / delete[] 在本章的最后一个问题。
3. 已释放的空间,不可重复释放
第一种最直接:
int* p = new int(71);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
cout << *p << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p; //OK!
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p; //ERROR! 重复删除p
第二种为重复删除同一指向的多个指针
int* p1 = new int(71);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int* p2 = p1; //p2和p1 现在指向同一内存地址
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
cout << *p1 << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
cout << *p2 << endl;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p1; //OK
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p2; //ERROR! p2所指的内存,已通过delete p1而被释放,不可再delete一次
同样的问题,如果你先删除了p2,则同样不可再删除p1
delete p2; //OK
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p1; //ERROR
第三种为删除指向某一普通变量的指针
int a = 100;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int* p = &a;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
delete p; //ERROR
Ctrl+Alt+Del进入任务管理器、性能,运行下列代码,并观察PF率的变化。可知,new运算符增加PF率,delete使PF率还原。注意:使用 new 得来的空间,必须用 delete 来释放;使用 new [] 得来的空间,必须用 delete [] 来释放。彼此之间不能混用。用 new [] 分配出连续空间后,指针变量“指向”该空间的首地址。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二. 指向连续空间的指针
在通过 new [] 指向连续空间以后,p 就变得和一个一维数组很是类似。我们先来复习一下数组相关知识。假设是这么一个数组: int arr[20]; 则arr 的内存示意图为
和指针变量相比,数组没有一个单独的内存空间而存放其内存地址。即:指针变量p是一个独立的变量,只不过它的值指向另一段连续的内存空间;而数组arr,本身代表的就是一段连续空间。
数组是“实”的地址,不能改变。当你和定义一个数组,则这个数组就得根据它在内存中的位置,得到一个地址,如上图中的“0x1A000000”。只要这个数组存在,那么它终生的地址就是这个值。
指针是一个“虚”的地址,可以改变地址的值。当你定义一个指针变量,这个变量占用4个字节的内存,你可以往这4字节的内存写入任意一个值,该值被当成一个内存地址。比如,你可以写入上面的“0x1A000000”,此时,指针p指向第一个元素。也可以改为“0x1A000003”,此时,指针p指向第二个元素。
所以,当p通过 new [] 指向一段连续空间的结果是,p 是一个指向数组的指针,而*p是它所指的数组。
两者的相似之处:
数组:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
指针:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
两者的不同之处:
数组:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
指针:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三. delete/delete[]的几个注意点
1. 指针通过 new 或 new[] ,向系统“申请”得到一段内存空间,这段内存空间必须在不需要将它释放了。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 一个指针被删除时,应指向最初的地址
当一个指针通过 +,- 等操作而改变了指向;那么在释放之前,应确保其回到原来的指向。如下所示:在 delete [] p 时,p指向的是第二个元素,结果该释放将产生错位:第一个元素没有被释放,而在最后多删除了一个元素。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第一种方法是把指针正确地"倒"回原始位置:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
但当我们的指针指向变化很多次时,在释放前要保证一步不错地一一退回,会比较困难。所以另一方法是在最初时“备份”一份。在释放时,直接释放该指针即可。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3. 已释放的空间,不可重复释放
第一种最直接:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第二种为重复删除同一指向的多个指针
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第三种为删除指向某一普通变量的指针
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
p 不是通过new 得到新的内存空间,而是直接指向固定变量a。所以删除p等同要强行剥夺a的固有空间,会导致出错。
因为我需要释放指向二维结构体指针的指针,为测试内存释放情况,改写程序。代码如下:
- #include<iostream>
- #include<stdio.h>
- using namespace std;
- void main()
- {
- cout << "指针变量占4字节内存。按回车键开始分配404M内存..." << endl;
- int temp;
- getchar();
- int x = 100;
- int y = 1024*1024;
- char ***p = new char** [y];
- for(int i=0; i<y; ++i)
- {
- p[i] = new char*[x];
- }
- cout << "内存分配完毕,按回车键开始清理内存..." << endl;
- getchar();
- for (int i=0; i<y; ++i)
- {
- delete []p[i];
- }
- cout << "二位数组,第二维清理完毕,共清理内存400M,剩余4M未清理,按回车键继续清理..." << endl;
- getchar();
- delete []p;
- p = NULL; //如果不对p赋值NULL,再次清理内存,将报错
- /*cout << "再次清理内存..." << endl;
- getchar();
- delete []p;
- p = NULL;*/
- cout << "内存清理完毕..." << endl;
- getchar();
- }
#include<iostream> #include<stdio.h> using namespace std; void main() { cout << "指针变量占4字节内存。按回车键开始分配404M内存..." << endl; int temp; getchar(); int x = 100; int y = 1024*1024; char ***p = new char** [y]; for(int i=0; i<y; ++i) { p[i] = new char*[x]; } cout << "内存分配完毕,按回车键开始清理内存..." << endl; getchar(); for (int i=0; i<y; ++i) { delete []p[i]; } cout << "二位数组,第二维清理完毕,共清理内存400M,剩余4M未清理,按回车键继续清理..." << endl; getchar(); delete []p; p = NULL; //如果不对p赋值NULL,再次清理内存,将报错 /*cout << "再次清理内存..." << endl; getchar(); delete []p; p = NULL;*/ cout << "内存清理完毕..." << endl; getchar(); }