当前位置:网站首页>new和malloc的区别
new和malloc的区别
2022-07-02 01:37:00 【wrdoct】
new和malloc区别(8个)
(1)new和delete是C++的关键字/运算符,malloc与free是c++/c语言的标准函数。
(2)malloc需要显式地指定分配的内存大小,new不需要。
(3)new操作符从自由存储区上为对象动态分配内存空间,而malloc函数从堆上动态分配内存。
【注】凡是通过new操作符进行内存申请,该内存即为自由存储区。
(4)new操作符内存分配成功时,返回对象类型,无须进行类型转换,故new是符合类型安全性的操作符;malloc返回void*,需要通过强制类型转换将void*指针转换成我们需要的类型。
(5)new操作符内存分配失败时,抛出bad_alloc异常;malloc内存分配失败时返回NULL。
(6)new操作符有构造函数和析构函数,在开辟空间的同时,会调用自定义对象的构造函数来完成初始化;malloc只会开辟空间。
(7)malloc分配空间后,可以通过realloc扩张内存;new操作符则不能进行再次扩张内存的操作。
(8)new相对malloc效率要低,因为new的底层封装了malloc。
边栏推荐
- Convolutional neural network (including code and corresponding diagram)
- What are the skills of spot gold analysis?
- 1218 square or round
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- [rust web rokcet Series 1] Hello, world and get, post, put, delete
- [Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
- No converter found for return value of type: class
- 学习笔记2--高精度地图定义及价值
- Feature extraction and detection 16 brisk feature detection and matching
- 学习笔记25--多传感器前融合技术
猜你喜欢
Exclusive delivery of secret script move disassembly (the first time)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
[Maya] the error of importing Maya into Metahuman
How can I batch produce the same title for the video?
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
Ks006 student achievement management system based on SSM
Learn C language from scratch day 025 (maze)
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
matlab 使用 resample 完成重采样
随机推荐
error: . repo/manifests/: contains uncommitted changes
遊戲思考15:全區全服和分區分服的思考
电子协会 C语言 1级 33 、奇偶数判断
Learn basic K-line diagram knowledge in three minutes
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
6-2漏洞利用-ftp不可避免的问题
Failed to transform file 'xxx' to match attributes
迁移云计算工作负载的四个基本策略
Discussion on the idea of platform construction
The technology boss is ready, and the topic of position C is up to you
[IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
Ks006 student achievement management system based on SSM
笔者更加愿意将产业互联网看成是一个比消费互联网要丰富得多的概念
Ubuntu20.04 PostgreSQL 14 installation configuration record
Basic number theory -- Gauss elimination
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
How to compress video size while adding watermark with one click?
Ubuntu20.04 PostgreSQL 14 installation configuration record
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?