当前位置:网站首页>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;
}
2. quote
2.1 Basic use of references
effect : Alias variables
grammar : data type & Alias = Original name
2.2 Notes on quotation
- References must be initialized
- Reference after initialization , Can't change
Example :
2.3 Reference as function parameter
effect : When a function passes parameters , You can use the technique of reference to let formal parameters modify actual parameters
advantage : You can simplify pointers and modify arguments
Example :
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
swap(a,b);
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
system("pause");
return 0;
}
Output results :a=20 b=10
2.4 Reference as function return value
effect : References can exist as function return values
Be careful : Do not return local variable references
usage : Function calls as lvalues
Example :
int& test01()
{
int a = 10;// local variable
return a;
}
// Returns a static variable reference
int& test02()
{
static int a = 10;
return a;
}
int main()
{
// Cannot return references to local variables
int& ref = test01();
cout<<"ref = "<<ref<<endl;// Output is 10, Because the compiler keeps
cout<<"ref = "<<ref<<endl;// Output random number
// If the function is left valued , Then you have to return the reference
int& ref2 = test02();
cout<<"ref2 = "<<ref2<<endl;// Output 10
test02()=1000;// Function to do lvalue
cout<<"ref2="<<ref2<<endl;// Output 1000
system("pause");
return 0;
}
2.5 The essence of quotation
The essence : The essence of quotation is c+ + The internal implementation is a pointer constant

2.6 const reference
effect : Constant references are mainly used to modify formal parameters , Prevent misoperation
In the function parameter list , You can add const Modified parameter , Prevent formal parameters from changing arguments

Ongoing update
边栏推荐
- Cdga | how can the industrial Internet industry do a good job in data governance?
- CDGA|工业互联网行业怎么做好数据治理?
- The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
- leetcode day3 查找重复的电子邮箱
- 基于MATLAB的函数拟合
- How openocd directly downloads programs to STM32 board through stlink (solved)
- Leetcode day3 employees who exceed the manager's income
- [NPP installation plug-in]
- Leetcode Day2 consecutive numbers
- [网络]跨区域网络的通信学习路由表的工作原理
猜你喜欢

NetCoreAPI操作Excel表格
![[wechat applet development] page navigation and parameter transmission](/img/10/76b6592fa9e71073831887c4fb6da9.png)
[wechat applet development] page navigation and parameter transmission

Saltstack system initialization

河北:稳粮扩豆助力粮油生产提质增效

How does app automated testing achieve H5 testing

Edge detection and connection of image segmentation realized by MATLAB

There is a 'single quotation mark' problem in the string when Oracle inserts data

数字图像理论知识(一)(个人浅析)

Prometheus deployment

How navicate modifies the database name
随机推荐
MySQL performance testing tool sysbench learning
NetCoreAPI操作Excel表格
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
2022年下半年系统集成项目管理工程师认证8月20日开班
毕马威中国:证券基金经营机构信息技术审计项目发现洞察
Leetcode Day1 score ranking
Data system of saltstack
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
STC12C5A60S2 function description (STC12C5A60S2 is triggered by default)
Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
Android-第十三节03xUtils-数据库框架(增删改查)详解
Leetcode day4 the highest paid employee in the Department
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
Saltstack system initialization
MySQL command statement (personal summary)
The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
Salt SSH of saltstack
Saltstack configuration management
【微信小程序开发】页面导航与传参
Deploy ZABBIX automatically with saltstack