当前位置:网站首页>New and malloc
New and malloc
2022-07-25 17:38:00 【Wildcraner】
new and malloc difference (8 individual )
(1)new and delete yes C++ Key words of / Operator ,malloc And free yes c++/c The standard function of language .
(2)malloc You need to explicitly specify the allocated memory size ,new Unwanted .
class A{…}
A * ptr = new A;
A * ptr = (A *)malloc(sizeof(A)); // You need to explicitly specify the required memory size sizeof(A);
(3)new The operator dynamically allocates memory space for objects from free storage , and malloc Function dynamically allocates memory from the heap . Free storage is C++ be based on new An abstract concept of an operator , Usually through new Operator for memory request , This memory is the free storage area . And heap is an operating system term , It is a special memory maintained by the operating system , For dynamic allocation of program memory ,C Language use malloc Allocate memory from the heap , Use free Release the allocated memory . So can a free storage area be a heap ( The problem is equivalent to new Whether memory can be allocated dynamically on the heap ), It depends. operator new Implementation details . Free storage can be more than just a heap , It can also be static storage , It's all up to you operator new Where to allocate memory for objects
(4)new When operator memory allocation succeeds , Return object type , No type conversion required , so new Is a type safe operator ;malloc return void*, You need to cast void* The pointer is converted to the type we need .
(5)new When the operator memory allocation fails , Throw out bad_alloc abnormal ;malloc Return when memory allocation fails NULL.
(6)new Operators have constructors and destructors , While opening up space , The constructor of the custom object will be called to complete the initialization ;malloc It will only open up space .
(7)malloc After allocating space , Can pass realloc Expand memory ;new Operator cannot expand memory again .
(8)new relative malloc Low efficiency , because new The bottom layer encapsulates malloc.
边栏推荐
- 计算日期或日期格式化
- 【无标题】
- 走马卒
- WPF 实现用户头像选择器
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- Headless mode of new selenium4.3 in egde browser
- Redis源码与设计剖析 -- 15.RDB持久化机制
- WPF 实现用户头像选择器
- 新版selenium4.3在egde浏览器的无头模式
- Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!
猜你喜欢

Redis cluster deployment based on redis6.2.4

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解

Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

对灰度图像的三维函数显示

交友活动记录

ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity

11、照相机与透镜

Outlook tutorial, how to search for calendar items in outlook?

精彩记录
随机推荐
大型仿人机器人的技术难点和应用情况
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
论文阅读_多任务学习_MMoE
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
Highlights
Lvgl 7.11 tileview interface cycle switching
Interface automation test postman+newman+jenkins
Add batch delete
How to prevent the unburned gas when the city gas safety is alarmed again?
stm32F407------SPI
WPF implements user avatar selector
Redis cluster deployment based on redis6.2.4
Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
01. Sum of two numbers
交叉验证(cv)学习笔记
世界各地的标志性建筑物
new与malloc