当前位置:网站首页>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
边栏推荐
- Redis如何实现多可用区?
- Thinking about the best practice of dynamics 365 development collaboration
- ctf. Show PHP feature (89~110)
- National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
- [Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
- [flask] official tutorial -part3: blog blueprint, project installability
- How to improve the level of pinduoduo store? Dianyingtong came to tell you
- 【网络攻防实训习题】
- Campus second-hand transaction based on wechat applet
- Leetcode skimming questions_ Sum of squares
猜你喜欢
C web page open WinForm exe
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
Leetcode skimming questions_ Sum of squares
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
Jisuanke - t2063_ Missile interception
500 lines of code to understand the principle of mecached cache client driver
Leetcode skimming questions_ Verify palindrome string II
UE4 unreal engine, editor basic application, usage skills (IV)
leetcode3、实现 strStr()
随机推荐
Online reservation system of sports venues based on PHP
D22:indeterminate equation (indefinite equation, translation + problem solution)
Using SA token to solve websocket handshake authentication
How to improve the level of pinduoduo store? Dianyingtong came to tell you
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Win10 add file extension
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
Extracting key information from TrueType font files
Redis list
Leetcode skimming questions_ Invert vowels in a string
Ali test open-ended questions
leetcode-2. Palindrome judgment
leetcode3、实现 strStr()
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
Paddle framework: paddlenlp overview [propeller natural language processing development library]
selenium 等待方式
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
MCU lightweight system core
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset