当前位置:网站首页>C language improvement (I)
C language improvement (I)
2022-07-29 02:17:00 【Tandy12356_】

Why is it commonly used C Language code instead of assembly code ?
It is mainly the machine code directly corresponding to the assembly language , If the platform is different , He can't run , and C The language is different , Use C Code written in , Use different compilers on different platforms to compile , The final generated assembly code happens to be able to run on the current platform , So his transplantation is better .

In principle, take as much as you use , Avoid the phenomenon of memory being wasted


The code area is read-only , Any code that wants to modify the contents of the read-only area , Will be killed by the operating system ( Reporting an error makes it collapse ), At the same time, the code area is also shared , The main purpose of doing this is to realize the program to open more shared memory resources , Realize the maximum utilization of memory .

Variables in stack space are usually temporary variables , We generally have no way to control , As the function call ends , The memory in stack space will also be recycled . Automatic application , Automatic release .

The memory at compile time is only the allocated address , Only the real runtime will really allocate memory .
The global static area generally includes : Global area 、 Static zone 、 The constant area
extern int a; // It can be used in other files
static int a; // It can only be used in the current file
Their life cycle runs through the whole process
String constant "hello" Generally, it is placed in the constant area
Overall const Variables are also placed in the constant area , Once initialized, the pointer cannot be modified
int* myFun(){
int a=10;
return &a;
}
After the function is called a The value of has no meaning , Be sure to pay attention to a The life cycle of is only meaningful inside the function , Without the function, it's meaningless , We don't care about the value obtained by calling the function , Because this memory will be recycled automatically .
Because you will make mistakes after using this variable , So don't return the local address of the variable !

After the function call ,str The content saved in it is recycled , So it will print out a bunch of garbled codes
As long as it is a continuous memory , Can use subscript to access memory
for In circulation , Recommended ++i, It's more efficient

The memory applied in the heap must get the first address , Only in this way can we release
p Of memory will be recycled , But use ret The first memory address that can receive the application
After using the heap memory, you must release it in time , And set the pointer to NULL
When defining variables, you must remember to initialize , quite a lot bug The generation of is due to the lack of initialization
void allocSpace(char* p) {
p = (char*)malloc(100);
memset(p, 0, 100);
strcpy(p, "hello");
}
int main() {
char* p = NULL;
allocSpace(p);
cout << p << endl;
return 0;
}The following problems occurred : Abnormal reading memory , because p yes NULL

// stay main Function
char* p = NULL;
00182065 mov dword ptr [ebp-8],0
allocSpace(p);
0018206C mov eax,dword ptr [ebp-8]
0018206F push eax
00182070 call 001814E7
00182075 add esp,4
// stay allocSpace Function
p = (char*)malloc(100);
00181951 mov esi,esp
00181953 push 64h
00181955 call dword ptr ds:[0018D1FCh]
0018195B add esp,4
0018195E cmp esi,esp
00181960 call 00181299
00181965 mov dword ptr [ebp+8],eax After follow-up, I found two p It's not the same thing at all , In different functions , Their addresses are separate , They have nothing to do with each other

Lost the address of heap memory , Caused a memory leak
In a function, its parameters , Local variables are assigned a unique space , It has nothing to do with other functions
Solution :
void allocSpace02(char**p) {
char* tmp = (char*)malloc(100);
memset(tmp, 0, 100);
strcpy(tmp, "hello");
*p = tmp;
}
int main() {
char* p = NULL;
allocSpace02(&p);
cout << p << endl;
return 0;
}
Using a pointer with a star can also store the address of the lower pointer (&p) So why use a secondary pointer ?
answer : Note if the designer wants you to be able to distinguish what type of data the pointer points to , If you use one star , I can't tell whose address is stored in your pointer , Is it an address of ordinary data type or an address of pointer type .


An unresolved external symbol g_a: The connector is reporting an error , Can't find in each file g_a, But the compilation can pass
The header file is full of declarations, not definitions
Any direct assignment of pointers of different types will alarm / Report errors ,void* With the exception of
overall situation const Put it in the constant area , Once initialized , Can no longer be modified
local variable , Constants, whether they are constants or not, are stored on the stack
Program running normally :


Program running error :

String constants are readable but not writable ↑

Function call :


For a frequently called and relatively short function , Macro functions are recommended ( Space for time ), Because it has no cost of ordinary functions : Function stack 、 Jump 、 Return, etc .
P20 The growth direction of stack and memory storage direction

The printed result address is upward in the direction of decreasing the surface stack

Low address storage low is the small end mode ,num The label is at the low address
verification :

边栏推荐
- Mathematical modeling -- the laying of water pipes
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- Why can't Bi software do correlation analysis
- E-commerce keyword research helps data collection
- [simple implementation and extension of one · data | array heap]
- Promise solves asynchrony
- H5 background music is played automatically by touch
- Solution of Lenovo notebook camera unable to open
- h5背景音乐通过触摸自动播放
- Mathematical modeling -- red wine quality classification
猜你喜欢
![[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)](/img/07/d5861404a76a0fb7d6d5f930a4a66a.png)
[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)

Understand the working principle of timer in STM32 in simple terms
![[one · data | chained binary tree]](/img/83/d62a47f1264673f1e898335303a7a6.png)
[one · data | chained binary tree]

Complete collection of common error handling in MySQL installation

Force deduction brush question (1): sum of two numbers
![[云原生]微服务架构是什么](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[云原生]微服务架构是什么

Motionlayout -- realize animation in visual editor

自定义mvc原理和框架实现

基于C51实现数码管的显示

Character flow comprehensive exercise problem solving process
随机推荐
The growth path of embedded engineers
QT learning notes -37.qregex and regular expressions
Navigation--实现Fragment之间数据传递和数据共享
[electronic components] zener diode
自定义mvc原理和框架实现
Mathematical modeling -- bus scheduling optimization
Control the pop-up window and no pop-up window of the input box
Semiconductor chip industry chain
Custom MVC principle and framework implementation
Cookies and sessions
Related function records about string processing (long-term update)
Probability Density Reweight
[云原生]微服务架构是什么
弹性布局 单选
JVM内存溢出在线分析Dump文件以及在线分析打开.hprof文件得出JVM运行报告jvisualvm怎么在线分析
【ONE·Data || 数组堆简单实现及其延伸】
Vector similarity evaluation method
[cloud native] what is the microservice architecture
Problems encountered in special flow & properties property set instances and Solutions
C语言提高篇(一)