当前位置:网站首页>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
边栏推荐
- UE4 unreal engine, editor basic application, usage skills (IV)
- 【Flask】获取请求信息、重定向、错误处理
- Apicloud openframe realizes the transfer and return of parameters to the previous page - basic improvement
- Dynamics 365 开发协作最佳实践思考
- [flask] response, session and message flashing
- [Jiudu OJ 09] two points to find student information
- 剑指 Offer 12. 矩阵中的路径
- Computer graduation design PHP enterprise staff training management system
- Mongodb problem set
- 安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
猜你喜欢
Basic operations of databases and tables ----- primary key constraints
Basic operations of database and table ----- set the fields of the table to be automatically added
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Basic operations of databases and tables ----- unique constraints
How to upgrade kubernetes in place
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
1. Introduction to basic functions of power query
UE4 unreal engine, editor basic application, usage skills (IV)
PHP campus financial management system for computer graduation design
随机推荐
Ali test open-ended questions
2022 PMP project management examination agile knowledge points (8)
Dynamics 365 开发协作最佳实践思考
Cookie concept, basic use, principle, details and Chinese transmission
Using SA token to solve websocket handshake authentication
Reasonable and sensible
Basic operations of database and table ----- set the fields of the table to be automatically added
MCU lightweight system core
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Sword finger offer 12 Path in matrix
Redis list
[flask] official tutorial -part3: blog blueprint, project installability
genius-storage使用文档,一个浏览器缓存工具
Folio. Ink is a free, fast and easy-to-use image sharing tool
Pangolin Library: subgraph
Initialize MySQL database when docker container starts
01.Go语言介绍
leetcode-2. Palindrome judgment
Publish your own toolkit notes using NPM