运算符 new 使用起来要比函数 malloc 简单得多,例如: int *p1 = (int *)malloc(sizeof(int) * length); int *p2 = new int[length]; 这是因为 new 内置了 sizeof、类型转换和类型安全检查功能。

对于非内部数据类型 的对象而言,new 在创建动态对象的同时完成了初始化工作。如果对象有多个构造函数, 那么 new 的语句也可以有多种形式。

 #include <iostream>

 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
//声明数组、变量和指针变量
int a[]={,,,,,};
int *ip1,*ip2; //测试指针的赋值运算
ip1=a;
ip2=ip1;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl; //测试指针的自增自减运算和组合运算
ip1++;
ip2+=;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl; //测试指针变量之间的关系运算
int n=ip2>ip1;
cout<<"ip2>ip1="<<n<<endl;
cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl; //指针变量之间的减法
n=ip2-ip1;
cout<<"ip2-ip1="<<n<<endl;
return ;
}

new/delete 的使用要点的更多相关文章

  1. c/c++ 复习基础要点01-const指针、指针函数 函数指针、new/delete与malloc/free区别与联系

    1.      引用本身是有指针实现的:引用为只读指针 例子: int d=123; int& e=d;    //引用 int * const e=d; //只读指针,e指向d,不可修改e指 ...

  2. malloc/free与new/delete的区别

    相同点:都可用于申请动态内存和释放内存 不同点:(1)操作对象有所不同.malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符.对于非内部数据类的对象而言,光用m ...

  3. malloc/free和new/delete的区别

    转自:http://blog.csdn.net/chance_wang/article/details/1609081 malloc与free是C++/C语言的标准库函数,new/delete是C++ ...

  4. malloc/free和new/delete的异同

    一.基本概念 malloc/free: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返 ...

  5. C++中malloc/free和new/delete 的使用

    malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...

  6. C/C++语言的标准库函数malloc/free与运算符new/delete的区别

    概括地说 1.malloc与free是C++/C的标准库函数,new/delete是C++的运算符,它们都可用于申请动态内存和释放内存. 2.对于非内部数据类型的对象而言,只用malloc/free无 ...

  7. malloc/free与new/delete的区别与联系

    相同点:(1)都是申请内存,释放内存,free和delete可以释放NULL指针:(2)都必须配对使用,这里的配对使用,可不能理解为一个new/malloc就对应一个delete/free,而是指在作 ...

  8. malloc/free 与 new/delete 比较

    相同点:都可用于申请动态内存和释放内存 不同点: (1)操作对象有所不同. malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符.对于非内部数据类的对象而言,光 ...

  9. C++/C语言的标准库函数与运算符的区别new/delete malloc/free

    malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++ ...

随机推荐

  1. django 连接mysql 数据库

    1.新建一个mysite项目:django-admin startproject mysite 2.进入项目目录,新建一个app : python manage.py startapp polls 3 ...

  2. novas的verdi和debussy是干什么用的(关于debussy的一些介绍)

    source code window: 提供了一个比较友好的界面,将整个设计的source code按设计的层次结构以树状排布,并且可以在代码上反标仿真结果,支持查找.寻找驱动等一些debug常用的操 ...

  3. Linux系统中 Sublime Text 中文 GBK 文件乱码问题

    Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就 ...

  4. linux 打包为zip压缩包

    [root@nb linux学习]# zip -r dir.zip dir文件夹/ file文件 adding: dir文件夹/ (stored %) adding: file文件 (deflated ...

  5. keepalived openssl 报错

    configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include Open ...

  6. Facebook提出DensePose数据集和网络架构:可实现实时的人体姿态估计

    https://baijiahao.baidu.com/s?id=1591987712899539583 选自arXiv 作者:Rza Alp Güler, Natalia Neverova, Ias ...

  7. merge源表数据移植到目标表新表数据中

    merge into dbo.ak_SloteCardTimes a using(select RecordID,CardNO,SloteCardTime from dbo.Tb_CardDate b ...

  8. Ubuntu 安装谷歌拼音输入法

    Ubuntu14.04内置的拼音输入法有一个bug,使得Live USB全拼无法使用. 安装Google拼音输入法步骤如下: 1. 打开Ubuntu软件中心 (Ubuntu Software Cent ...

  9. nyoj576 集齐卡片赢大奖(一)

    集齐卡片赢大奖(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 小时候你一定曾经为收集一套三国人物的卡片而买过不少零食吧?这些小吃的袋子里一般都会有一张卡片,如 ...

  10. yii2 关系...

    link($name) 会使用..get$name...即有hasmany和hasone... // update lazily loaded related objects if (!$relati ...