当前位置:网站首页>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。
边栏推荐
- SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
- Docker安装Oracle_11g
- Altium designer measure distance (ctrl+m)
- k线图形态这样记(口诀篇)
- 微信小程序中使用tabBar
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
- 只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
- error: . repo/manifests/: contains uncommitted changes
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
猜你喜欢

Exclusive delivery of secret script move disassembly (the first time)

Hcip day 14 (MPLS protocol)

6-3漏洞利用-SSH环境搭建
![[disease detection] realize lung cancer detection system based on BP neural network, including GUI interface](/img/c9/3fe8693629a8452dcfdb4349ddee0d.png)
[disease detection] realize lung cancer detection system based on BP neural network, including GUI interface
![Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation](/img/eb/b1382428d6578b8561d7fcc1a2a5cd.jpg)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation

Raspberry pie 4B learning notes - IO communication (1-wire)

Principle of finding combinatorial number and template code

Docker安装Oracle_11g

How to compress video size while adding watermark with one click?
![[IVX junior engineer training course 10 papers to get certificates] 0708 news page production](/img/ad/a1cb672d2913b6befd6d8779c993ec.jpg)
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production
随机推荐
2022年6月国产数据库大事记
Convolutional neural network (including code and corresponding diagram)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
6-3漏洞利用-SSH环境搭建
电子协会 C语言 1级 33 、奇偶数判断
Implementation principle of city selector component
遷移雲計算工作負載的四個基本策略
How can I batch produce the same title for the video?
Introduction to ffmpeg Lib
Electronic Society C language level 1 32, calculate the power of 2
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
城市选择器组件实现原理
The technology boss is ready, and the topic of position C is up to you
1217 supermarket coin processor
三分钟学会基础k线图知识
No converter found for return value of type: class
Ubuntu20.04 PostgreSQL 14 installation configuration record
I Brief introduction of radio energy transmission technology
Unity AssetBundle subcontracting