程序员人生 网站导航

C++中new与delete的用法

栏目:php教程时间:2014-12-08 08:46:28

最近在学习C++的进程中,发现指针和new和delete的使用遍及全书,可见其重要性。在学习了1个阶段以后,总结1下new和delete的用法,指针还没有摸透,待往后总结。

1.new和delete有甚么用?

new和delete是C++中的两个操作符,new用于给单个对象或数组,结构等分配内存,在内存使用完后,用delete进行释放内存。而且是逐一对应关系,有了new分配了内存,必定需要delete来释放内存,其分配和释放的必须是同1地址的内存。不然就很有可能致使内存泄漏的后果,如果内存泄漏严重,程序就极有可能崩溃。

2.使用new创建动态数组

在这里顺便介绍3个术语:

(1)联编(binding):联编是指1个计算机程序本身彼此关联(使1个源程序经过编译、连接,成为1个可履行程序)的进程,在这个联编进程中,需要肯定程序中的操作调用(函数调用)与履行该操作(函数)的代码段之间的映照关系,依照联编所进行的阶段不同,可分为静态联编和动态联编。

(2)静态联编(static binding):是指在编译阶段就将函数实现和函数调用关联起来,因此静态联编也叫早绑定,在编译阶段就必须了解所有的函数或模块履行所需要检测的信息,它对函数的选择是基于指向对象的指针(或援用)的类型。

(3)动态联编(dynamic binding):指在程序履行的时候才将函数实现和函数调用关联,因此也叫运行时绑定或晚绑定,动态联编对函数的选择不是基于指针或援用,而是基于对象类型,不同的对象类型将做出不同的编译结果。


然后我们假定需要编写1个程序,其中需要的数组长度没法在编译时进行肯定,而需要在运行时期肯定其中的值,这样就没法通过声明1个数组满足需求了。由于声明数组后,程序在编译期间将会为它分配内存,不管程序终究是不是使用过数组,这个数组都占用了1定的内存,而且它不够灵活。所以在这类情况下,我们就需要使用动态联编。也是用使用new创建动态数组,在运行时分配内存。

代码实例:

#include <iostream> int main() { using namespace std; int * point = new int[3]; //int * point[3] = new int; //This is a wrong way to difine a array point[0] = 1; point[1] = 2; point[2] = 3; cout << "point[0] is " << point[0] << " "; cout << "point[1] is " << point[1] << " "; cout << "point[2] is " << point[2] << endl; //endl is equal to " " delete [] point; //free memory return 0; }

这段代码简单易懂,但凡是有点编程基础的人应当都能看懂。其中最关键的两句不过就是new分配内存和delete释放内存的语句。new表达式分配了1个含有3个元素的数组,并且指针指向数组第1个元素,数组名此时表示数组第1个元素的地址。


3.使用new创建动态结构

创建动态结构和创建动态数组的方式和思想并没有太大的出入。直接上实例,在代码中分析。

struct struct1 //structure definition { char name[20]; float volume; double price; }; int main() { using namespace std; struct1 * ps = new struct1; //allot memory for structure cout << "Enter name of struct1 item: "; cin.get(ps->name,20); cout << "Enter volume in cubic feet: "; cin >> (*ps).volume; //method 2 for memory access cout << "Enter price: $"; cin >> ps -> price; cout << "Name: " << (*ps).name << endl; cout << "Volume: " << ps->volume << " cubic feet "; //method 1 cout << "Price: $" << ps->price << endl; //method 2 delete ps; return 0; }
简单说几点和创建动态数组的不同的地方:

<1>创建数组时的指针类型可以根据需求不同可以是int,char,double等等,创建结构时指针类型就只能是结构变量了,也就是上述代码中的struct1,不能任意的喜欢那个类型就上哪一个了。指针方面的不同就不分析了

<2>对创建数组,分配内存和释放内存时都有1个[],但是在结构中,就没有这个[]了。

4.使用new创建单个对象

动态创建对象时,只需指定其数据类型,没必要为该对象命名,new表达式返回指向新创建对象的指针,我们通过该指针来访问此对象。

int main() { using namespace std; int nights = 1001; int * pt = new int; //allocate space for an int *pt = 1001; //store a value there cout << "nights value = "; cout << nights << ": location " << &nights << endl; cout << "int "; cout << "value = " << *pt << ": location = " << pt << endl; double * pd = new double; //allocate space for a double *pd = 10000001.0; //store a value there cout << "double "; cout << "value = " << *pd << ": location = " << pd << endl; cout << "location of pointer pd: " << &pd << endl; cout << ": size of * pt = " << sizeof(*pt) << endl; cout << "size of pd = " << sizeof pd; cout << ": size of *pd = " << sizeof(*pd) << endl; return 0; }
最后简单提1下这段代码中的nights变量和pd指针的值都寄存在栈(stack)中,而用new分配的内存在堆(heap)或自由存储区(free store)中。




------分隔线----------------------------
------分隔线----------------------------

最新技术推荐