当前位置:网站首页>Bug memory management
Bug memory management
2022-06-26 13:48:00 【Hua Weiyun】
C++11 Initializing new gameplay for members of
C++11 Supports initialization and assignment of non static member variables when declared ,== But note that this is not initialization , Here is the default value for the declared member variable .==
Understand encapsulation again
C++ It's an object-oriented program ,== Object oriented has three major features, namely : encapsulation 、 Inherit 、 polymorphic ==.
C++ By class , Combine the properties and behavior of an object , Make it more consistent with people's cognition of a thing , Package everything that belongs to the object ; Selectively open some of its functions through access qualifiers to interact with other objects , For some implementation details inside the object , External users do not need to know , I see. It doesn't work in some cases , Instead, it increases the difficulty of use or maintenance , Complicate the whole thing .
Understand object orientation again
It can be seen that object-oriented is actually simulating the abstract mapping of the real world
C/C++ memory management
C/C++ Distribution of memory
Let's take a look at the following code and related problems
== Virtual process address space ==
【 explain 】
- Stack Also called stack , Non static local variables / Function parameter / Return value and so on , The stack is growing down .
- Memory mapped segments It's efficient I/O How to map , For loading a shared dynamic memory library . Users can use the system interface to create shared data
Shared memory , Do interprocess communication .(Linux If you don't learn this in the course , Now just need to know )- Pile up Dynamic memory allocation for program runtime , The heap can grow up .
- Data segment – Store global and static data .
- Code segment – Executable code / Read only constants .
C Dynamic memory management in language
==malloc/calloc/realloc and free==
【 Interview questions 】
malloc/calloc/realloc The difference between ?
C++ Memory management
C Language memory management is C++ You can continue to use , But there are some places that can't be helped, and it's troublesome to use , therefore C++ It also puts forward its own memory management mode :== adopt new and delete Operator for dynamic memory management ==.
new/delete Operation built-in type
int main(){ //c Language application 10 individual int Array of int* pa = (int*)malloc(sizeof(int)*10); //c++ apply 10 individual int Array of int* pb = new int[10]; //c Language free space free(pa); //c++ Release space delete[] pb; //c Language application 1 individual int int* a = (int*)malloc(sizeof(int)); //c++ apply 1 individual int int* b = new int; //c Language free space free(a); //c++ Release space delete b; return 0;}
struct ListNode{ ListNode* _next; ListNode* _prev; int _val; ListNode(int val = 0) :_next(nullptr) , _prev(nullptr) ,_val(val) { cout << "ListNode(int val = 0" << endl; } ~ListNode() { cout << "ListNode()" << endl; }};int main(){ //c Language to create linked list nodes struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode)); //free Is to free up space directly free(n1); //c++ Create a linked list node ListNode* n2 = new ListNode(10); //delete It is to deconstruct and clean up before releasing space delete n2; return 0;}
Be careful : When applying for a custom type of space ,new Constructor will be called ,delete Will call the destructor , and malloc And free Can't .
struct ListNode{ ListNode* _next; ListNode* _prev; int _val; ListNode(int val = 0) :_next(nullptr) , _prev(nullptr) , _val(val) { cout << "ListNode(int val = 0" << endl; } ~ListNode() { cout << "ListNode()" << endl; }};int main(){ //c Language to create linked list nodes struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode)); //free Is to free up space directly free(n1); //c++ Create a linked list node ListNode* n2 = new ListNode[10]{1,2,3,4,5,6,7,8,9,0}; //delete It is to deconstruct and clean up before releasing space delete[] n2; return 0;}
边栏推荐
- Input text to automatically generate images. It's fun!
- Connection migration for DataGrid configuration
- Wechat applet -picker component is repackaged and the disabled attribute is added -- above
- Mysql database explanation (III)
- 7-1 range of numbers
- 33、使用RGBD相机进行目标检测和深度信息输出
- 基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
- I have a good word to say, and I admire myself
- Applicable and inapplicable scenarios of mongodb series
- Luogu p3426 [poi2005]sza-template solution
猜你喜欢

HW蓝队溯源流程详细整理

There are many contents in the widget, so it is a good scheme to support scrolling

【Spark】. Explanation of several icons of scala file in idea

What is the use of index aliases in ES

NVM installation tutorial

character constants

ES6 module

What features are added to Photoshop 2022 23.4.1? Do you know anything

8. Ribbon load balancing service call

Postman自动化接口测试
随机推荐
嵌入式virlog代码运行流程
KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Calculate the distance between two points (2D, 3D)
Logical operation
Cloudcompare - Poisson reconstruction
Es snapshot based data backup and restore
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
泰山OFFICE技术讲座:使用字体粗体的四种情形
[path of system analyst] Chapter 15 double disk database system (database case analysis)
Variable declaration of typescript
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
计算两点之间的距离(二维、三维)
[node.js] MySQL module
Design of simple digital circuit traffic light
7.consul service registration and discovery
Nexys A7开发板资源使用技巧
Reprint - easy to use wechat applet UI component library
Generation and rendering of VTK cylinder
MySQL explanation (II)













