当前位置:网站首页>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;}
边栏推荐
- Nlp-d60-nlp competition D29
- LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
- CVPR 2022文档图像分析与识别相关论文26篇汇集简介
- awk工具
- 创建一个自己的跨域代理服务器
- 虫子 类和对象 中
- Zero basics of C language lesson 7: break & continue
- Reprint - easy to use wechat applet UI component library
- Solutions to the failure of last child and first child styles of wechat applet
- Calculate the distance between two points (2D, 3D)
猜你喜欢

Cloudcompare - Poisson reconstruction

Range of types

去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程

Lamp compilation and installation

Here Document免交互及Expect自动化交互

基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类

Detailed introduction to shell script (4)

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

MediaPipe手势(Hands)

输入文本自动生成图像,太好玩了!
随机推荐
[how to connect the network] Chapter 1: the browser generates messages
7.consul service registration and discovery
Cloudcompare - Poisson reconstruction
虫子 STL string 下 练习题
7-3 minimum toll
12 SQL optimization schemes summarized by old drivers (very practical)
团队管理的最关键因素
mysql配置提高数据插入效率
Global variable vs local variable
A primary multithreaded server model
shell脚本详细介绍(四)
Stack, LIFO
character constants
Solutions to the failure of last child and first child styles of wechat applet
7-16 monetary system I
Generation and rendering of VTK cylinder
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
Update and download of Beifu EtherCAT XML description file
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示













