当前位置:网站首页>0211 embedded C language learning
0211 embedded C language learning
2022-07-06 01:59:00 【int H】
The pointer
A pointer is a data type , It can define variables , The value saved by the variable is the address , Because the address is a fixed length , So the length of pointer variable is fixed ; The steps of different addresses are different , Variables of different pointer types are required to save
*: Indirect operators
&: Fetch address operator ( Corresponding memory space , Memory space pointed to )
(“&” and “*” It's all right combined . Assumed variable x = 10, be *&x The meaning is , First get the variable x The address of , And get the content in the address . because “&” and “*” They are inverses of each other , therefore x = *&x.)
p++:p The corresponding address is added by itself
(*p)++:p The content of the address pointed to is added by itself
When to use multi-level pointers ?
Application scenarios : Function arguments
Wild pointer
a) What is the wild pointer ?
Define uninitialized pointer
After the release, the pointer
Pointer for out of bounds access
Such as : char *p
Wild pointer : There is no access to the corresponding space of the memory address in the pointer variable ( The space pointed to by the pointer variable has no access rights )
b) Problems caused by wild pointer ?
Memory leak --- Runtime error --- Memory error ( Segment error )
c) Precautions for wild pointer :
1、 Pointer to constant store object
2、 Resource leakage
3、 Memory out of bounds
4、 The return value is a pointer
5、 Pointer as formal parameter
d) How to avoid wild pointer ?
Develop good coding habits :
1) When defining pointer variables, you must initialize :
When pointer variables are used as pointing tools , It is initialized as NULL;
When assigning a value to the space pointed to by the pointer variable , Need to give dynamic application space
2) When using :
Check whether the memory space is allocated successfully
Initialize memory space
Prevent cross-border access
3) At the end of use :
You have to free up space
After release, be sure to initialize the pointer to NULL
NULL:#define NULL (void *)0
NULL It stands for 0 Address ( Cannot access , Cannot store data segment )
char *p = NULL;//EOF
Be careful : Wild pointer cannot be eliminated , It can only be avoided !!!
malloc、free、calloc、realloc:
a)malloc:
void * malloc(size_t size);
This function can open up continuous available memory space on the heap :
If the development is successful , Then return a pointer to open a good space
If the development fails , Then return to NULL The pointer , therefore malloc The return value of must be checked
Because the type of the return value is void *, therefore malloc Function doesn't know the type of open space
The specific time of use is determined by the user himself
If parameters size by 0,malloc The standard of behavior is undefined , Depends on the compiler
b)free:
void free(void * ptr);
If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined
If parameters ptr yes NULL The pointer , Then the function doesn't have to do anything
c)calloc:
void * callloc(size_t num,size_t size);
calloc And malloc The only difference in the development method of is to initialize while opening up memory
d)realloc:
void * realloc(void * ptr,size_t size);
realloc Function can adjust the size of dynamic development memory
ptr Is the memory address to be adjusted size The return value of the new size after adjustment is the actual location of the memory after adjustment
This function adjusts the size of the original memory space , It will also move the data in the original memory to a new space :
1) There is enough space behind the original space
2) There is not enough space after the original space

边栏推荐
- Flutter Doctor:Xcode 安装不完整
- Leetcode skimming questions_ Sum of squares
- [technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
- Pangolin Library: subgraph
- Social networking website for college students based on computer graduation design PHP
- Publish your own toolkit notes using NPM
- Mongodb problem set
- 阿里测开面试题
- Computer graduation design PHP college student human resources job recruitment network
- 2022 PMP project management examination agile knowledge points (8)
猜你喜欢

Extracting key information from TrueType font files

Computer graduation design PHP enterprise staff training management system

Mongodb problem set

Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail

SPI communication protocol

Derivation of Biot Savart law in College Physics

国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!

Open source | Ctrip ticket BDD UI testing framework flybirds

Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail

C web page open WinForm exe
随机推荐
Computer graduation design PHP part-time recruitment management system for College Students
[le plus complet du réseau] | interprétation complète de MySQL explicite
Initialize MySQL database when docker container starts
Derivation of Biot Savart law in College Physics
Publish your own toolkit notes using NPM
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
Numpy array index slice
【网络攻防实训习题】
Force buckle 1020 Number of enclaves
Format code_ What does formatting code mean
2022 PMP project management examination agile knowledge points (8)
NiO related knowledge (II)
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Unity learning notes -- 2D one-way platform production method
Ali test Open face test
How does redis implement multiple zones?
晶振是如何起振的?
[Jiudu OJ 09] two points to find student information
leetcode-2.回文判断
Paddle framework: paddlenlp overview [propeller natural language processing development library]