当前位置:网站首页>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;}
边栏推荐
猜你喜欢

NVM installation tutorial

Es6: iterator

Analysis of state transition diagram of Beifu NC axis

Global variable vs local variable

Detailed sorting of HW blue team traceability process

Chapter 10 setting up structured logging (2)

Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value

Logical operation

Free machine learning dataset website (6300+ dataset)

Design of simple digital circuit traffic light
随机推荐
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
I have a good word to say, and I admire myself
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
5+api, clear application cache
网络远程访问的方式使用树莓派
Chapter 10 setting up structured logging (2)
MongoDB系列之Window环境部署配置
Mysql database explanation (6)
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
虫子 内存管理 上
Zero basics of C language lesson 8: Functions
ES6 module
微信小程序注册指引
D中不用GC
Echart stack histogram: add white spacing effect setting between color blocks
Range of types
Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
Use of wangeditor rich text editor
GC is not used in D
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism













