当前位置:网站首页>New keyword learning and summary
New keyword learning and summary
2022-04-23 16:54:00 【The interview was rejected 10000 times】
new Operator
Reviewing c And review c++ in , Always put new Keyword to simply allocate memory , Obviously not enough .
First of all, I learned from two blogs .
new Create an array of object pointers ,
new
《c++ primer plus》
- The real use of pointers is Dynamic memory allocation , Allocate unnamed memory space during runtime
- typeName* pointer_name = new typeName;
typeName Tell the compiler , Used to specify what kind of ( What size ) Memory space and what kind of pointer to declare- new and Delete Must be used together , Release Memory requested by heap or free storage
Static binding and Dynamic linking
Define static binding
: In large data , For example, array , If you create memory at the time of declaration , No use , It always takes up memory , Like this in Allocating memory during compilation is called static chaining
> int tacos[10];
Define dynamic binding
: Use new when , If you need an array at run time , Specify the length of the array to allocate memory , This kind of Determining memory space during runtime is called dynamic binding
int* pz = new int[10];
delete [] pz;
In the actual project , Some frequently used memory space , It doesn't release , Initialize after use , Wait until the assignment is activated next time and use
new and malloc The difference between
- malloc and free yes c Standard library functions for languages ,new and delete It's the operator
- Why add new and delete?
In non built-in data types (class class ) In terms of objects c++ Built in type , You need to call the constructor and destructor .
new Call the constructor of the object while creating the object in memory .
delete Call the memory destructor when cleaning up memory .( It's very important ) - By default new[] Allocated memory , Give it to delete[] To release
- Default operator new() Allocated memory , Give it to operator delete To release
new Create objects
class Person {
int age ;
public:
Person(/* args */){
age = 0;
cout << " Default constructor " << endl;
} ;
Person(int val):age(val) { cout << " Constructor with parameters " << endl; };
int getPerson() const { return age; };
~Person(){};
};
int main()
{
int ret = 0;
Person tep = Person(); // The stack area , Default structure
Person* pa = new Person; // Without brackets , Heap area , Default structure
ret = pa->getPerson();
delete pa;
Person* pc = new Person(); // Bracketed , Heap area , Default structure , It is not the default parameter construction with parentheses
ret = pc->getPerson();
delete pa;
Person* pb = new Person(100); // There are parametric structures
ret = pb->getPerson();
delete pb;
}
new Create an array of pointers to the object
When looking at the project code , Created a TcpScoket[m_nSocketNum] Object array for , Every element is TcpScoke object ,
So if you create an array of pointers , You can create these nodes in the heap ( When used in the project , None of this space will be released , Difference one
One on the stack , Just one on the pile ), use new How to create a pointer array ?
class Person{ };
int main()
{
int nSize = 10;
Person* pa = new Person[nSize]; // Open up an array element for Person Of memory space ,
// open up Person Pointer array of , How to open up
// I can't write this as new (Person*)[nSize] , Also can not It's written in new Person(*[nSize])
Person** pb = new Person*[nSize]; // Opening up an array element is (Person*) Of memory space
for( int i=0; i<nSize ;i++)
{
pb[i] = new Person(); // to Person Open up space
}
// Release space
for( int i=0; i<nSize ;i++)
{
delete pb[i];
}
delete[] pb;
}
版权声明
本文为[The interview was rejected 10000 times]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359046402.html
边栏推荐
- JMeter installation tutorial and solutions to the problems I encountered
- Pycham connects to the remote server and realizes remote debugging
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- NVIDIA显卡驱动报错
- Use case execution of robot framework
- RTKLIB 2.4.3源码笔记
- UWA Pipeline 功能详解|可视化配置自动测试
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- Construction of promtail + Loki + grafana log monitoring system
- loggie 源码分析 source file 模块主干分析
猜你喜欢
Shell脚本——Shell编程规范及变量
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Do you really understand the principle of code scanning login?
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
NVIDIA显卡驱动报错
Change the password after installing MySQL in Linux
磁盘管理与文件系统
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
oracle 中快速获取表的列名列表
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
随机推荐
Detailed explanation of UWA pipeline function | visual configuration automatic test
Construction of promtail + Loki + grafana log monitoring system
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
True math problems in 1959 college entrance examination
LVM and disk quota
如何建立 TikTok用户信任并拉动粉丝增长
手写事件发布订阅框架
网络安全之渗透靶场实战详解
Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
The new MySQL table has a self increasing ID of 20 bits. The reason is
Disk management and file system
websocket
Zhimeng dedecms security setup Guide
磁盘管理与文件系统
Calculate pie chart percentage
PHP efficiently reads large files and processes data
Linux MySQL data timing dump
Deepinv20 installation MariaDB
[problem solving] [show2012] random tree
织梦DEDECMS安全设置指南