当前位置:网站首页>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 :

边栏推荐
- QT learning notes -37.qregex and regular expressions
- 多线程浅谈
- 全志T3/A40i工业核心板,4核[email protected],国产化率达100%
- 12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance
- Basic working principle and LTSpice simulation of 6T SRAM
- RGBD点云降采样
- Excel 打开包含汉字的 csv 文件出现乱码该怎么办?
- Lm13 morphological quantification momentum period analysis
- Cookies and sessions
- 点击回到顶部js
猜你喜欢

基于C51控制蜂鸣器

Have you ever encountered the situation that the IP is blocked when crawling web pages?

STM32 DMA receives serial port data

Ciscn 2022 central China Misc

第十四天:续第十三天标签相关知识

autoware中ndtmatching功能加载点云图坐标系修正的问题

(arxiv-2018) reexamine the time modeling of person Reid based on video

druid. io kill -9 index_ Realtime traceability task

2022年编程语言排名,官方数据来了,让人大开眼界

In 2022, the official data of programming language ranking came, which was an eye opener
随机推荐
表单校验 隐藏的输入框 显示才校验
[one · data | chained binary tree]
JetPack--Navigation实现页面跳转
全志T3/A40i工业核心板,4核[email protected],国产化率达100%
[circuit design] open collector OC output of triode
Mathematical modeling -- bus scheduling optimization
What is scope and scope chain
(arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模
[circuit design] peak voltage and surge current
防止重复点击
“蔚来杯“2022牛客暑期多校训练营2,签到题GJK
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
Mathematical modeling - location of police stations
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
druid. IO custom real-time task scheduling policy
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
Excel 用countif 统计
Related function records about string processing (long-term update)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
Mathematical modeling -- cold proof simulation of low temperature protective clothing with phase change materials