c++ new() 与new[]的区别转自http://blog.sina.com.cn/s/blog_52e27e8a0100rr24.html
来源:互联网 发布:整型数据 编辑:程序博客网 时间:2024/05/12 20:59
转自http://blog.sina.com.cn/s/blog_52e27e8a0100rr24.html
c++ new() 与new[]的区别 (2011-06-07 22:22:37)转载▼
标签: c new delete 杂谈 分类: 网络经典new和delete运算符用于动态分配和撤销内存的运算符
new用法:
1. 开辟单变量地址空间
1)new int; //开辟一个存放数组的存储空间,返回一个指向该存储空间的地址.int *a = new int 即为将一个int类型的地址赋值给整型指针a.
2)int *a = new int(5) 作用同上,但是同时将整数赋值为5
2. 开辟数组空间
一维: int *a = new int[100];开辟一个大小为100的整型数组空间
二维: int **a = new int*[5]
for()
{a[i] = new int[6];}
三维及其以上:依此类推.
一般用法: new 类型 [初值]
delete用法:
1. int *a = new int;
delete a; //释放单个int的空间
2.int *a = new int[5];
delete [] a; //释放int数组空间
要访问new所开辟的结构体空间,无法直接通过变量名进行,只能通过赋值的指针进行访问.
用new和delete可以动态开辟,撤销地址空间.在编程序时,若用完一个变量(一般是暂时存储的数组),下次需要再用,但却又想省去重新初始化的功夫,可以在每次开始使用时开辟一个空间,在用完后撤销它.
0 0
- c++ new() 与new[]的区别转自http://blog.sina.com.cn/s/blog_52e27e8a0100rr24.html
- IL与CLR两个概念(转自http://blog.sina.com.cn/s/blog_49947b280100gtde.html)
- 视频测试序列的下载地址 转自:http://blog.sina.com.cn/s/blog_5fd6a0e50100dn0k.html
- Kosaraju算法的分析和证明 转自http://blog.sina.com.cn/s/blog_4dff87120100r58c.html
- Android中Uri的使用转自http://blog.sina.com.cn/s/blog_821e2bb10100spxv.html
- opencv的一个神经网络例程(转自http://blog.sina.com.cn/s/blog_61e10f020101bv94.html)
- freemarker的使用,转自http://blog.sina.com.cn/s/blog_64c505480100sxuh.html
- 抽象类与接口的区别--http://blog.sina.com.cn/s/blog_5e9f4ac60100ddfq.html
- Python String和PyQt QString的区别(转) http://blog.sina.com.cn/s/blog_4b5039210100h9kp.html
- ldap安装与学习(转载自:http://blog.sina.com.cn/s/blog_72ee04a40100p0ce.html)
- 用Eclipse CDT 配置C/C++ 编译环境(转自http://blog.sina.com.cn/s/blog_45dbe005010009jf.html)
- core dump 相关保存(转自http://blog.sina.com.cn/s/blog_602f87700100ew04.html)
- 借鉴:CFD建模计算 (转自 http://blog.sina.com.cn/s/blog_497df1da0100akzt.html )
- bed文件格式(转自http://blog.sina.com.cn/s/blog_70b2b6020100liou.html)
- ExecutorService线程池 转自 http://blog.sina.com.cn/s/blog_6151984a0100krj8.html
- vim set 转自http://blog.sina.com.cn/s/blog_7ae240850100x7ov.html
- log4net配置(转自 http://blog.sina.com.cn/s/blog_6d1c583c01010tf3.html )
- train test 划分 转自 http://blog.sina.com.cn/s/blog_6a90ae320101a5rc.html
- C语言访问MCU寄存器的三种方式
- xelatex 中文排版与字体更换
- 大端模式与小端模式
- Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析
- 负载均衡调度算法
- c++ new() 与new[]的区别转自http://blog.sina.com.cn/s/blog_52e27e8a0100rr24.html
- java中的线程同步问题 模拟出售火车票
- Nginx负载均衡和LVS负载均衡的比较分析
- Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析
- HDU1532Drainage Ditches(网络流入门+EK模板题)
- BadgeView源码分析
- 自学QT之自己写一个窗口
- Centos7 安装zabbix-server
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析