当前位置:网站首页>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.
边栏推荐
- I2C communication - sequence diagram
- The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
- 【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
- Boring post roast about work and life
- Virtual memory management
- I'm also drunk. Eureka delayed registration and this pit!
- Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
- [Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
- Redis源码与设计剖析 -- 17.Redis事件处理
- 03. Longest substring without repeated characters
猜你喜欢

How to install govendor and open a project

吴恩达机器学习编程作业无法暂停pause问题解决

OSPF---开放式最短优先路径协议

精彩记录

世界各地的标志性建筑物

Wu Enda logistic regression 2

Installation steps and usage of NVM under windows10 system

Ultimate doll 2.0 | cloud native delivery package

【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
随机推荐
Page table cache of Linux kernel source code analysis
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
EDI docking commercehub orderstream
Three dimensional function display of gray image
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?
EDI 对接CommerceHub OrderStream
Cross validation (CV) learning notes
对灰度图像的三维函数显示
理财有保本产品吗?
自动化测试 PO设计模型
Automated test Po design model
go defer与recover简单笔记
"Digital security" alert NFT's seven Scams
Chapter VI succession
【无标题】
PostgreSQL里有只编译语句但不执行的方法吗?
I'm also drunk. Eureka delayed registration and this pit!
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
栈的顺序存储结构,链式存储结构及实现
go接口变量的类型断言