当前位置:网站首页>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
边栏推荐
- Basic operations of databases and tables ----- default constraints
- 晶振是如何起振的?
- ClickOnce does not support request execution level 'requireAdministrator'
- SPI communication protocol
- 2022年PMP项目管理考试敏捷知识点(8)
- How to set an alias inside a bash shell script so that is it visible from the outside?
- NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
- D22:indeterminate equation (indefinite equation, translation + problem solution)
- 【SSRF-01】服务器端请求伪造漏洞原理及利用实例
- A Cooperative Approach to Particle Swarm Optimization
猜你喜欢
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Publish your own toolkit notes using NPM
Extracting key information from TrueType font files
leetcode3、实现 strStr()
02.Go语言开发环境配置
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
TrueType字体文件提取关键信息
Computer graduation design PHP college classroom application management system
Social networking website for college students based on computer graduation design PHP
Computer graduation design PHP part-time recruitment management system for College Students
随机推荐
Basic operations of databases and tables ----- default constraints
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Computer graduation design PHP animation information website
SPI communication protocol
Text editing VIM operation, file upload
PHP campus movie website system for computer graduation design
Virtual machine network, networking settings, interconnection with host computer, network configuration
Publish your own toolkit notes using NPM
抓包整理外篇——————状态栏[ 四]
Sword finger offer 38 Arrangement of strings
MCU lightweight system core
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
leetcode-2. Palindrome judgment
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
FTP server, ssh server (super brief)
Regular expressions: examples (1)
selenium 元素定位(2)
leetcode-2.回文判断