当前位置:网站首页>Memory model, reference and function supplement of program
Memory model, reference and function supplement of program
2022-06-12 05:38:00 【Violence produces miracles】
List of articles
Partition model of memory
c++ When the program executes, the memory is roughly 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 , Recycle by the operating system at the end of the program
After the program is compiled , The first section that generates the execution of the program 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 are stored here
The global area also contains a constant area , Character 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
Heap area :
Heap data is managed and released by programmers
Heap data utilization new Keyword to open up memory
new The operator
The heap area opens up a number , It's the address , Receive... With a pointer
Example
int *p = new int(10) // In parentheses are numbers
The heap opens up an array , It's the address , Receive... With a pointer
Example
int *arr = new int[10] // In parentheses is the array size
After that, the access is normal ,arr Equivalent to array name
For releasing data delete, Where the array is released with delete[] arr
quote
You can alias variables , Use aliases to manipulate variables
grammar : data type & Alias = Original name
for example :
int a = 10
int &b = a
You can use it b To operate a The numerical
matters needing attention
- References must be initialized
- Reference after initialization , Can't change
Three kinds of transmission
Value passed , Parameter changes do not affect arguments
void fun(int a, int b)
fun(a,b)
Address delivery , Parameter changes affect arguments
void fun(int *a, int *b)
fun(&a, &b)
reference , Parameter changes affect arguments
void fun(int &a, int &b)
fun(a,b)
Reference as function return value
effect : A reference can exist as the return value of a function
Be careful : Do not return local variable references
usage : Function calls can be used as lvalues
for example
int& test(){
static int a = 20;
return a;
}
test() = 1000;// It is equivalent to modifying the contents of the returned reference
The essence of quotation
The essence of quotation is c++ The internal implementation is a pointer constant
const reference
int& ref = 10;// This is wrong , The reference itself needs a legal memory space
const int& ref = 10 That's right , The reason is that the compiler optimizes the code
It's equivalent to executing int temp = 10; const int& ref = temp;
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
Function addition
Function default parameter considerations :
1、 If a location parameter has a default value , All parameters from this position must have default values
2、 If the function declaration has default arguments , Function implementations cannot have default parameters , Only one declaration and implementation can have default parameters
function overloading :
1、 The same scope
2、 The function names are the same
3、 Function parameter types are different , Or different numbers , Or in a different order
4、 The default parameter will be ambiguous , Report errors , To avoid
边栏推荐
- Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
- Please remove any half-completed changes then run repair to fix the schema history
- GRE protocol details
- merge sort
- WiFi protocol and ieee905 protocol learning details
- Beginning is an excellent emlog theme v3.1, which supports emlog Pro
- Necessary for Test Engineer -- package capturing tool fiddler
- Simple introduction to key Wizard
- flex/fixed上中下(移動端)
- The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
猜你喜欢

ESP8266 Arduino OLED

利用jieba库进行词频统计

16. Somme des trois plus proches

FPGA语法的细节

Nature | 给全球的新冠伤亡算一笔账

yolov5

The most commonly used objective weighting method -- entropy weight method

Serial port oscilloscope_ port_ Setup of plotter secondary development environment (including QT setup)

Matlab: halftone and dither conversion

Identification of campus green plants based on tensorflow
随机推荐
Kubernetes certificate online update
Simple introduction to key Wizard
Beginning is an excellent emlog theme v3.1, which supports emlog Pro
Automated testing - Po mode / log /allure/ continuous integration
Halcon 3D 1 读取3d数据
AddUser add user and mount hard disk
44. a digit in a sequence of digits
[speech] how to customize ring back tone according to different countries
Thesis reading_ Figure neural network gin
国企为什么要上市
beginning一款非常优秀的emlog主题v3.1,支持Emlog Pro
Rtmp/rtsp/hls public network real available test address
About architecture (in no particular order)
Performance test - GTI application service performance monitoring platform
利用jieba库进行词频统计
FPGA语法的细节
Webrtc AEC process analysis
Multi thread learning III. classification of threads
flex/fixed上中下(移动端)
51. reverse order pairs in the array