new/delete 的使用要点
运算符 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 的使用要点的更多相关文章
- c/c++ 复习基础要点01-const指针、指针函数 函数指针、new/delete与malloc/free区别与联系
1. 引用本身是有指针实现的:引用为只读指针 例子: int d=123; int& e=d; //引用 int * const e=d; //只读指针,e指向d,不可修改e指 ...
- malloc/free与new/delete的区别
相同点:都可用于申请动态内存和释放内存 不同点:(1)操作对象有所不同.malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符.对于非内部数据类的对象而言,光用m ...
- malloc/free和new/delete的区别
转自:http://blog.csdn.net/chance_wang/article/details/1609081 malloc与free是C++/C语言的标准库函数,new/delete是C++ ...
- malloc/free和new/delete的异同
一.基本概念 malloc/free: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返 ...
- C++中malloc/free和new/delete 的使用
malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...
- C/C++语言的标准库函数malloc/free与运算符new/delete的区别
概括地说 1.malloc与free是C++/C的标准库函数,new/delete是C++的运算符,它们都可用于申请动态内存和释放内存. 2.对于非内部数据类型的对象而言,只用malloc/free无 ...
- malloc/free与new/delete的区别与联系
相同点:(1)都是申请内存,释放内存,free和delete可以释放NULL指针:(2)都必须配对使用,这里的配对使用,可不能理解为一个new/malloc就对应一个delete/free,而是指在作 ...
- malloc/free 与 new/delete 比较
相同点:都可用于申请动态内存和释放内存 不同点: (1)操作对象有所不同. malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符.对于非内部数据类的对象而言,光 ...
- C++/C语言的标准库函数与运算符的区别new/delete malloc/free
malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++ ...
随机推荐
- MySQL Cluster 具体配置文件(config.ini)
########################################################################### ## MySQL CLuster 配置文件 ## ...
- python 中hive 取日期时间的方法
#!/usr/bin/env python3 import sys import os import time, datetime sys.path.append(os.getenv('HIVE_TA ...
- git 录制简单实用好工具 LICEcap
官网 https://www.cockos.com/licecap/ 界面如图: 录制效果如下:
- 修改 Input placeholder 的样式
::-webkit-input-placeholder { /* WebKit browsers */ color: #ccc; } :-moz-placeholder { /* Mozilla Fi ...
- JavaScript——DOM或以树形展示的Web页面
Web网页的一般能够通过document以及document所相关的各种元素组成.当然我们也能够通过层次结构的树形结构在展现Web页面.假设要对一个网页进行改动的话,我们能够通过document对象. ...
- Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个 在切面类中,有5种通知类型: aop:before 前置通知 aop:after-returning 后置通知 aop:after 最终通知 aop:af ...
- 每日英语:Is It Possible To Reason About Having A Child?
How can you decide whether to have a child? It's a complex and profound question -- a philosophical ...
- 终端模拟工具:Xshell 4
终端模拟工具:Xshell 4 2016-09-20 目录 1 安装 2 配置 3 命令 Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Window ...
- 重装Ubuntu系统
1.安装JDK参考:http://weixiaolu.iteye.com/blog/1401786jdk-6u31-linux-i586.bin莫名奇妙的安装失败.所以下载了jdk-7u45-linu ...
- CentOS 7 开放防火墙端口 命令
iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 servi ...