当前位置:网站首页>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。
边栏推荐
- Electronic Association C language level 1 33, odd even number judgment
- [Chongqing Guangdong education] Tianshui Normal University universe exploration reference
- 站在新的角度来看待产业互联网,并且去寻求产业互联网的正确方式和方法
- Learn C language from scratch day 025 (maze)
- 电商系统中常见的9大坑,你踩过没?
- SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
- Architecture evolution from MVC to DDD
- Implementation of Weibo system based on SSM
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
猜你喜欢
机器学习基本概念
Feature extraction and detection 16 brisk feature detection and matching
【LeetCode 43】236. The nearest common ancestor of binary tree
Docker installing Oracle_ 11g
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
学习笔记2--高精度地图定义及价值
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
What is AQS and its principle
6-3漏洞利用-SSH环境搭建
学习笔记25--多传感器前融合技术
随机推荐
Android: the kotlin language uses grendao3, a cross platform app development framework
10 minutes to get started quickly composition API (setup syntax sugar writing method)
error: . repo/manifests/: contains uncommitted changes
Learn about servlets
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
Look at the industrial Internet from a new perspective and seek the correct ways and methods of industrial Internet
Edge extraction edges based on Halcon learning_ image. Hdev routine
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
浅浅了解Servlet
[IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
Penser au jeu 15: penser au service complet et au sous - service
城市选择器组件实现原理
游戏思考15:全区全服和分区分服的思考
6-2漏洞利用-ftp不可避免的问题
II Basic structure of radio energy transmission system
Feature extraction and detection 16 brisk feature detection and matching
[IVX junior engineer training course 10 papers] 06 database and services
matlab 使用 resample 完成重采样
Docker安装Oracle_11g
Error creating bean with name ‘stringRedisTemplate‘ defined in class path re