c++--new和delete

目录

使用new来分配内存

使用delete释放内存

使用new来创建动态数组

使用new创建动态结构


使用new来分配内存

在使用new时,程序员要告诉new,需要为哪种数据类型分配内存;new将找到一个长度正确的内存块,并返回该内存的地址。程序员的责任是将该地址赋给一个指针。

例如,在运行阶段为一个int值分配未命名的内存,并使用指针来访问这个值:

int *pn = new int;

为一个数据对象(可以是结构,也可以是基本类型)获得并指定分配内存的通用格式如下:

typeName * pointer_name = new typeName;
/* use_new.cpp--using the new operator */
#include <iostream>
#include <string>
using namespace std;

int main()
{
    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 double there
    cout << "double ";
    cout << "value = " << *pd << ":location = " << pd << endl;
    cout << "location of pointer pd: " << &pd <<endl;
    cout << "size of  pt = " << sizeof(pt);
    cout << ": size of *pt = " << sizeof(*pt) << endl;
    cout << "size of pd = " << sizeof(pd);
    cout << ": size of *pd = " << sizeof(*pd) << endl;
    return 0;
}


nights value = 1001:location 0x6dfef8
int value = 1001: location = 0x73a178
double value = 1e+007:location = 0x73a218
location of pointer pd: 0x6dfef4
size of  pt = 4: size of *pt = 4
size of pd = 4: size of *pd = 8

使用delete释放内存

当需要内存时,可以使用new来请求,另一个是delete运算符,它使得在使用完内存后,能够将其归还给内存池。归还或释的内存可供程序的其他部分使用。使用delete时,后面要加上指向内存块的指针(这些内存最初是用new分配的):

int * ps = new int; //allocate memory with new
...        //use the memory
delete ps; //free memory with delete when done

这将释放ps指向的 内存,但不会删除指针ps本身。

只能用delete来释放使用new分配的内存。然而,对空指针使用delete是安全的。

使用new来创建动态数组

例如,要创建一个包含10个int元素的数组:

int *psome  = new int [10]; //get a block of 10 ints

new运算符返回第一个元素的地址。

对于使用new创建的数组,应使用另一种格式的delete来释放:

delete [] psome; //free a dynamic array
/* arraynew.cpp--using the new operator for arrays*/
#include <iostream>
#include <string>
using namespace std;

int main()
{
    double *p3 = new double [3]; //space for 3 double
    p3[0] = 0.2;
    p3[1] = 0.5;
    p3[2] = 0.8;
    cout << "p3[1] is " << p3[1] << ".\n";
    p3 = p3 + 1; //increment the pointer
    cout << "Now p3[0] is " << p3[0] << " and ";
    cout << "p3[1] is " << p3[1] << ".\n";
    p3 = p3 - 1; //point back to beginning
    delete [] p3; //free the memory
    return 0;
}

p3[1] is 0.5.
Now p3[0] is 0.5 and p3[1] is 0.8.

使用new创建动态结构

/* newstruct.cpp--using new with a structure*/
#include <iostream>
#include <string>
using namespace std;
struct inflatable
{
    char name[20];
    float volume;
    double price;
};

int main()
{
    inflatable *ps = new inflatable; //allocate memory for structure
    cout << "Enter name of inflatable item: ";
    cin.get(ps->name, 20);
    cout << "Enter volume in cubic feet: ";
    cin >> (*ps).volume;
    cout << "Enter price: $";
    cin >> ps->price;
    cout << "Name: " << (*ps).name << endl;
    cout << "Volume: " << ps->volume << " cubic feet\n";
    cout << "Price: $" << ps->price << endl;
    delete ps; //free memory used by structure
    return 0;
}

Enter name of inflatable item: Fabulous Frodo
Enter volume in cubic feet: 1.4
Enter price: $27.99
Name: Fabulous Frodo
Volume: 1.4 cubic feet
Price: $27.99

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值