当前位置:网站首页>How class only static allocation and dynamic allocation
How class only static allocation and dynamic allocation
2022-08-04 03:36:00 【Can't beat the little ants】
Concept
- Static allocation
The compiler allocates memory in stack space for objects.Using this method, the constructor of the class is called directly.For example:
A a
- Dynamic build
Allocate memory in heap space.This process is divided into two steps:
[first step] execute the operator new( ) function to allocate memory in the heap space;
[second step] call the class constructor to construct the object.
Static allocation
Ideas
- To restrict the new operator to implement class objects can only be created on the stack.This restricts the class object from calling the new operator.
class A {private:void* operator new(size_t t){} //Set as privatevoid operator delete(void* ptr){} //If new is overloaded, delete needs to be overloaded.Corresponds to overloading.public:A(){}~A(){}};
Dynamic allocation
There are a few points to note.
- Only allowing dynamic allocation requires disallowing direct calls to constructors.But the constructor needs to be called indirectly, so the constructor cannot be made private.
- When the compiler allocates stack space for a class object, it will first check the accessibility of the class's destructor (in fact, not only the destructor, but as long as it is a non-static function, the compiler willan examination).If the class's destructor is not accessible outside the class, the compiler refuses to allocate memory for the class object on stack space.This also disables static allocation.Therefore, destructors can be defined as private.
- Because the destructor is defined as private, an additional destructor needs to be defined to release memory.
class A {public:A(){}void destory(){delete this;}private:~A(){}};
边栏推荐
- Functions, recursion and simple dom operations
- Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
- 自定义通用分页标签01
- base address: environment variable
- Polygon zkEVM网络节点
- 4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- JVM的内存模型简介
- mq应用场景介绍
- [Playwright Test Tutorial] 5 minutes to get started
猜你喜欢
基地址:环境变量
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
JVM内存和垃圾回收-07.堆
逻辑漏洞----其他类型
一文详解DHCP原理及配置
企业直播风起:目睹聚焦产品,微赞拥抱生态
Oracle与Postgresql在PLSQL内事务回滚的重大差异
C language -- ring buffer
docker+bridge+redis master-slave+sentry mode
三分建设,七分管理!产品、系统、组织三管齐下节能降耗
随机推荐
复制带随机指针的链表
一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型
Deep Learning (3) Classification Theory Part
使用serve搭建本地服务器
JVM的内存模型简介
Asynchronous programming solution Generator generator function, iterator iterator, async/await, Promise
打造一份优雅的简历
Brush esp8266-01 s firmware steps
一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
SQL注入中 #、 --+、 --%20、 %23是什么意思?
从图文展示到以云为核,第五代验证码独有的策略情报能力
一个属于程序员的七夕节!
Returns the maximum number of palindromes in a string
How many ways do you know about communication between multiple threads?
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
【项目实现】Boost搜索引擎
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Mockito单元测试
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
KingbaseES数据库启动失败,报“内存段超过可用内存”