当前位置:网站首页>C+ + core programming
C+ + core programming
2022-07-28 20:01:00 【Ordinary konjaku 99】
C+ + Programming core
1 Memory partition model
C+ + Program in execution , The main direction of memory is divided into 4 Regions
- Code section : Store the binary code of the function body , Managed by the operating system
- Global area : Store global and static variables and constants
- The stack area : Release is automatically allocated by the compiler , Stores the parameter values of the function , Local variables, etc
- Heap area : Assigned and released by the programmer , If programmers don't release , Retracted by the operating system at the end of the program
The meaning of four areas of memory :
Data stored in different areas , Give different life cycles , Give us more flexible programming
1.1 Before the program runs
After the program is compiled , Generated exe Executable program , Before the program is executed, it is divided into two areas :
- Code section : Deposit CPU Machine instructions executed
The code area is Shared , The purpose of sharing is for programs that are frequently executed , Just have a copy of the code in memory
The code area is read-only , The reason to make it read-only is to prevent the program from accidentally modifying its instructions
- Global area : Global variables and Static variables Store here
The global area also contains Constant District , String constants and other constants are also stored here . The data in this area is released by the operating system at the end of the program .
1.2 After program running
The stack area : Release is automatically allocated by the compiler , Stores the parameter values of the function , Local variables, etc
matters needing attention : Do not return the address of a local variable , The data opened in the stack area is automatically released by the compiler
Heap area : Assigned and released by the programmer , If programmers don't release , Retracted by the operating system at the end of the program
stay C+ + The main use of new Open up memory in the heap
int *func()
{
// utilize new keyword , Data can be opened up to the heap
// Pointers are also local variables in nature , Put it on the stack. , Pointer saved Put the data in the heap
int *p = new int (10);
return p;
}
int main()
{
// Open up data in the heap
int *p = func();
cout << *p << endl;
system("pause");
return 0;
}
The output of this code is 10
1.3 new The operator
C+ + of use new The operator creates data in the heap
Data from the development of the reactor area , It's created manually by the programmer , Hand release , Release with operator delete
utilize new Data created , Will return a pointer to the type corresponding to the data
new Basic syntax
int *func()
{
// Create shaping data in the heap
//new Returns a pointer to the data type
int *p = new int (10);
return p;
}
int main()
{
// The data in the heap area is managed and developed by the programmer , Release
// If you want to release data from the heap , With keywords delete
int *p = func();
cout << *p << endl;
delete p;
//cout << *P << endl; Memory has been freed , Illegal access again , Will report a mistake
system("pause");
return 0;
}
Open up an array in the heap
int *func()
{
// establish 10 An array of shaped data , In the pile area
int *arr = new int[10];
// The number in brackets is the length of the array
for(int i = 0; i<10; i++)
arr[i] = i+100;
for(int i = 0; i<10; i++)
cout<< arr[i] << endl;
// Free heap array , To add brackets
delete[] arr;
}
边栏推荐
- Overcome the "fear of looking at teeth", and we use technology to change the industry
- 并发程序设计,你真的懂吗?
- Leetcode day3 find duplicate email addresses
- Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- With the help of panel industry innovation, will FPGA become the best choice for TCON?
- [wechat applet development] page navigation and parameter transmission
- Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
- Leetcode Day5 delete duplicate email
- MySQL command statement (personal summary)
猜你喜欢

软考中级(系统集成项目管理工程师)高频考点

Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)

冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题

How to write the SQL statement of time to date?

“中国网事·感动2022”二季度网络感动人物评选结果揭晓

How does app automated testing achieve H5 testing

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

Cloud computing notes part.1 - system management

【微信小程序开发】页面导航与传参

毕马威中国:证券基金经营机构信息技术审计项目发现洞察
随机推荐
Idea properties file display \u solution of not displaying Chinese
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
Deploy LNMP automatically with saltstack
Servlet学习笔记
Codeignier framework implements restful API interface programming
Information management system and games based on C language
leetcode day1 分数排名
时间转日期的sql语句应该怎么写?
冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
How does app automated testing achieve H5 testing
数字滤波器设计——Matlab
Implementation of markdown editor in editor.md
What is the process of swing event processing?
In order to develop high-end photoresist, Jingrui Co., Ltd. invested 75million yuan to purchase SK Hynix ASML lithography machine
There are five certificates in the soft examination advanced examination, which is more worth taking?
English translation Arabic - batch English translation Arabic tools free of charge
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
[network] cross area network communication learning classification and calculation of IPv4 address
Common modules of saltstack
Left alignment function of Lua language (handwriting)