二维数组动态数组分配和释放
首先,数组指针和指针数组的区别:
<p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(0, 0, 255);">指针数组:</span>array of pointers,即用于存储指针的数组,也就是数组元素都是指针,指针的数组</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(0, 0, 255);">数组指针:</span>a pointer to an array,即指向数组的指针,数组的指针</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;">用法的区别:</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;">int* a[4] 指针数组 指针的数组</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"> 表示:数组a中的元素都为int型指针 </p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(255, 0, 0);"> 元素表示:*a[i] *(a[i])是一样的,因为[]优先级高于*</span></p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;">int (*a)[4] 数组指针 数组的指针 </p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"> 表示:指向数组a的指针</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(255, 0, 0);"> 元素表示:(*a)[i] </span></p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; line-height: 18px;">类似的还有指针函数和函数指针,以后再说</span>
</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;">然后,new 的用法 C++</p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(51, 51, 51); font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 23.799999237060547px; background-color: rgb(240, 240, 240);">和 sizeof 类似,new 和 delete 不是函数,它们都是 C++ 定义的关键字,通过特定的语法可以组成表达式。和 sizeof 不同的是,sizeof 在编译时候就可以确定其返回值,new 和 delete 背后的机制则比较复杂。</span></p><p style="line-height: 18px; background-color: rgb(255, 255, 255); margin: 10px auto; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;"><span style="color: rgb(51, 51, 51); font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 23.799999237060547px; background-color: rgb(240, 240, 240);">new 和 C 语言中的 malloc 函数一样,用来动态申请空间的。</span></p><p style="margin: 10px auto;"><span style="font-family:Helvetica, arial, freesans, clean, sans-serif;color:#333333;"><span style="line-height: 23.799999237060547px;">C 语言中 </span></span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; line-height: 24px;">void *malloc(int size);</span></p><p style="margin: 10px auto;"><span style="font-family:Helvetica, arial, freesans, clean, sans-serif;color:#333333;"><span style="line-height: 23.799999237060547px;"></span></span><pre id="recommend-content-19539850" accuse="aContent" class="recommend-text mb-10" style="margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; white-space: pre-wrap; word-wrap: break-word; font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; background-color: rgb(243, 255, 236);">说明:malloc 向系统申请分配指定size个字节的内存空间。返回类型是 void* 类型。void* 表示未确定类型的指针。
C,C++规定,void* 类型可以强制转换为任何其它类型的指针。 从函数声明上可以看出。malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小。
用free( ) 释放内存
void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针。如果分配失败,则返回一个空指针(NULL)。
关于分配失败的原因,应该有多种,比如说空间不足就是一种。
void free(void *FirstByte): 该函数是将之前用malloc分配的空间还给程序或者是操作系统,也就是释放了这块内存,让它重新得到自由。
C++中,new 用 delete 释放。
new int;//开辟一个存放整数的存储空间,返回一个指向该存储空间的地址(即指针)
new int(100);//开辟一个存放整数的空间,并指定该整数的初值为100,返回一个指向该存储空间的地址
new char[10];//开辟一个存放字符数组(包括10个元素)的空间,返回首元素的地址
new int[5][4];//开辟一个存放二维整型数组(大小为5*4)的空间,返回首元素的地址
float *p=new float (3.14159);//开辟一个存放单精度数的空间,并指定该实数的初值为//3.14159,将返回的该空间的地址赋给指针变量p
new运算符使用的一般格式为 new 类型 [初值]
用new分配数组空间时不能指定初值。
如果由于内存不足等原因而无法正常分配空间,则new会返回一个空指针NULL,用户可以根据该指针的值判断分配空间是否成功。 delete运算符使用的一般格式为 delete [ ] 指针变量 例如要撤销上面用new开辟的存放单精度数的空间(上面第5个例子),应该用 delete p; 前面用“new char[10];”开辟的字符数组空间,如果把new返回的指针赋给了指针变量pt,则应该用以下形式的delete运算符撤销该空间: delete [] pt;//在指针变量前面加一对方括号,表示是对数组空间的操作
<span style="background-color: rgb(240, 240, 240); font-family: Arial, Helvetica, sans-serif;">第一种:数组指针的内存分配和释放</span>
1、
char (*p)[N];//指向数组的指针
p = new char[M][N];
/*分配一个含有M*N个元素的二位数组,未被初始化*/
delete[] p;
/*释放内存*/
2、
char **p;//<span style="background-color: rgb(240, 240, 240); color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; line-height: 24px;">双重指针,<span style="font-family: arial; font-size: 13px; line-height: 20.020000457763672px;">指向</span><span style="color: rgb(204, 0, 0); font-family: arial; font-size: 13px; line-height: 20.020000457763672px;">int</span><span style="color: rgb(51, 51, 51); font-family: arial; font-size: 13px; line-height: 20.020000457763672px;">类型指针的指针,所以p的类型为一个指向整形的指针</span></span>
p = new char* [M];//分配指针数组
for(int i=0; i<M; i++)
p[i] = new char[N];//分配每个指针所指向的数组
for(i=0; i<M; i++)
delete[] p[i];
delete[] p;
第二种,指针数组的内存分配和释放
1、如上,指针的数组
char **p;
p= new char* [M];
p[0] = new char[M *N];//一次性分配所有空间
for(int i=1; i<M; i++)
p[i] = p[i-1] + N;//分配每个指针所指向的数组
delete[] p[0];
delete[] p;
2、
char* P[M];//指针的数组
for(int i=0; i<M; i++)
P[i] = new char[N]; //分配每个指针的空间
for(i=0; i<M; i++)
delete[] P[i];
3、
char* a[M];//指针的数组
a[0] = new char[M*n];<span style="background-color: rgb(240, 240, 240); font-family: Arial, Helvetica, sans-serif;">//分配每个指针的空间</span>
for(int i=1; i<M; i++)
a[i] = a[i-1] + n;<span style="background-color: rgb(240, 240, 240); font-family: Arial, Helvetica, sans-serif;">//分配每个指针所指向的数组</span>
delete[] a[0];
注意:
为了避免内存泄漏,new和delete要注意配对使用,即有多少个new就有多少个delete
何为内存泄漏?
answer:
内存泄漏也称作“存储渗漏”,用动态存储分配函数动态开辟的空间,在使用完毕后未释放,结果导致一直占据该内存单元。直到程序结束。(其实说白了就是该内存空间使用完毕之后未回收)即所谓内存泄漏。
内存泄漏形象的比喻是“操作系统可提供给所有进程的存储空间正在被某个进程榨干”,最终结果是程序运行时间越长,占用存储空间越来越多,最终用尽全部存储空间,整个系统崩溃。所以“内存泄漏”是从操作系统的角度来看的。这里的存储空间并不是指物理内存,而是指虚拟内存大小,这个虚拟内存大小取决于磁盘交换区设定的大小。由程序申请的一块内存,如果没有任何一个指针指向它,那么这块内存就泄漏了。
危害变现: