当前位置:网站首页>Wild pointer recurrence
Wild pointer recurrence
2022-07-28 17:33:00 【maosql】
List of articles
One 、 I don't know if the pointer is used incorrectly
I believe everyone has heard of wild pointer , I also know and I also know the harm . But I don't know that I always use wild pointer , Ashamed to speak .
Before using pointer ↓
// function : Read the contents of the pointer
// Realization : Output the contents of the pointer to the serial port assistant
// author :maosql
// Time :2021.9.9
void DisplayPointer(void)
{
char *p; //u8 *p;
const u8* zfc="masoql"; // Connect the password
sprintf((char*)p,"AT+XXX\"%s\"",zfc);// Set wireless parameters :ssid, password
}
Take it for granted that this string is stored in the pointer p among : First address , Save it back one by one , When I visit, the value in it should be the value I saved , In fact, it should be so , But there is a lot of uncertainty , Because such use is the wrong operation , Pointers cannot be assigned this way ;
error : You cannot assign a value like this without allocating memory space to the pointer , If p Later memory is used , The string cannot be assigned and the program crashes
consequence : This kind of program will appear , Maybe it's still good now , After a day , Nothing , You may not be able to run
Two : Wrong lead
Although the harm is great , But I don't know before , There is no problem with this use , Possible problems , I didn't think in this direction . When one day I need to access and output the content inside, I find that I can't access , I thought at first printf The output pointer is in the wrong format , I Baidu others said the output method is still wrong , Then the accidental experiment can be output , But it makes me puzzled
1. Read the error content
Correct printf Output pointer format , But the content read is h mouth ( My God . What is this !! See the screenshot below )
// function : Read the contents of the pointer
// Realization : Output the contents of the pointer to the serial port assistant
// author :maosql
// Time :2021.9.9
void DisplayPointer(void)
{
char *p; //u8 *p;
const u8* zfc="masoql"; // Simple string (??? Why can we assign values again ↓↓↓ )
/* eg: image char *p = "hello"; Such , It's not about defining a pointer p, Then give this p assignment , The content inside is filled with hello. It is hello Such a string , Exists in a place in memory , Then define a pointer , Point here */
sprintf((char*)p,"AT+XXX,\"%s\"",zfc);// hold AT+XXX,"maosql" Store in p In the memory of the first address
printf("%s\r\n",(char *)p); // || printf(p); || printf("%s\r\n",p);// Format can output content correctly
// printf("%p\r\n",&p);
}
result :( The code format is the same. For the convenience of explanation, the original code is not used )
2. Why read the content
The same output format as above is followed by a sentence , You can display the desired results (why, The following statement can also affect the previous statement ? See the screenshot below )
// function : Read the contents of the pointer
// Realization : Output the contents of the pointer to the serial port assistant
// author :maosql
// Time :2021.9.9
void DisplayPointer(void)
{
char *p; //u8 *p;
const u8* zfc="masoql"; // Connect the password
sprintf((char*)p,"AT+XXX\"%s\"",zfc);// Set wireless parameters :ssid, password
printf("%s\r\n",(char *)p); // printf(p); || printf("%s\r\n",p);
printf("%p\r\n",&p);
}
result : This result can be output correctly , But that's why , Why is it unreasonable to add this sentence after it , But it can output the results correctly , Until now, I haven't understood why . If someone understands , Please also comment on the area guidance 12 .( But I realized , The wrong starting point , There are strange problems , So I directly learn how to use it correctly )
3、 ... and : Wrong solution
Give up the wrong problem caused by the wrong use above , Directly use the method of correctly assigning values to pointers
1. Method 1 :
// function : Read the contents of the pointer
// Realization : Output the contents of the pointer to the serial port assistant
// author :maosql
// Time :2021.9.9
void DisplayPointer(void)
{
char p[64] = {
0};// The way to declare an array is to realize the function of a pointer , By manipulating the p Realize the use of pointers , As we all know, arrays 、 The function itself is the first address of the pointer
const u8* zfc="masoql"; // Simple string (??? Why can we assign values again ↓↓↓ )
/* eg: image char *p = "hello"; Such , It's not about defining a pointer p, Then give this p assignment , The content inside is filled with hello. It is hello Such a string , Exists in a place in memory , Then define a pointer , Point here */
sprintf((char*)p,"AT+XXX\"%s\"",zfc); // hold AT+XXX,"maosql" Store in p In the memory of the first address
printf("%s\r\n",(char *)p); // || printf(p); || printf("%s\r\n",p);// Format can output content correctly
}

2. Method 2 :
// function : Read the contents of the pointer
// Realization : Output the contents of the pointer to the serial port assistant
// author :maosql
// Time :2021.9.9
void DisplayPointer(void)
{
char *p; //u8 *p;
const u8* zfc="masoql"; // Simple string (??? Why can we assign values again ↓↓↓ )
/* eg: image char *p = "hello"; Such , It's not about defining a pointer p, Then give this p assignment , The content inside is filled with hello. It is hello Such a string , Exists in a place in memory , Then define a pointer , Point here */
p=mymalloc(SRAMIN,32); // apply 32 Byte memory
sprintf((char*)p,"AT+XXX\"%s\"",zfc); // hold AT+XXX,"maosql" Store in p In the memory of the first address
printf("%s\r\n",(char *)p); // || printf(p); || printf("%s\r\n",p);// Format can output content correctly
myfree(SRAMIN,p); // Free memory
p=NULL; // Avoid becoming a wild pointer
}
result : Output correctly , There are no strange problems 
Four : Understanding and perception after solution
Pointer to an address , Always for easy reading existing The contents of the address . If you want to point to, point to the address value of the existing memory address , If you want to assign a value to the pointer, use the above two methods , One is used as an array , A dynamic application for memory space ( Remember to release and set NULL).
边栏推荐
- Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
- Gray code and binary conversion and typical examples (4bits gray code counter)
- Selection and application of inductors in high speed circuits
- Goweb开发之Iris框架实战:项目总结与回顾
- The practice of beego framework developed by goweb: Section 4 database configuration and connection
- Proof of the third scene (f) in 22 years
- The practice of the beego framework of goweb development: Section II project initialization configuration
- 区分ES6的export与Nodejs的module.exports的区别
- WPF command button transparent style
- Verilog 每日一题 (VL27 可置位计数器)
猜你喜欢

Goweb开发之Beego框架实战:第五节 项目搭建及注册用户

异步FIFO基本原理(基于Verilog的简单实现)

Connection design and test platform -- Summary of SystemVerilog interface knowledge points

【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法

mysql实现按照自定义(指定顺序)排序

Valarray Library Learning

利用SQL Server代理作业对数据库进行定时还原

Encrypt the video and upload it to OSS to achieve high concurrent access

一文掌握 JVM 面试要点

Redis source code analysis, hold it hard, and code it quickly
随机推荐
【atlas】atlas 编译报错整理(全)
Encrypt the video and upload it to OSS to achieve high concurrent access
循环中使用switch在执行条件后使用break和continue的区别
Redis源码剖析,狠狠地拿捏了,赶紧码住
Verilog daily question (vl6 data series to parallel circuit)
Verilog 每日一题(VL26 简易秒表)
Shell编程之Sed
C语言实现扫雷小游戏
MySQL详细学习教程(建议收藏)
Verilog 每日一题 (VL28 加减计数器)
Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
创建自定义分页控件
高速电路中电阻的选择
Valarray Library Learning
The actual combat of the beego framework of goweb development: Section III program execution process analysis
Verilog daily question (vl28 plus and minus counter)
C # traversal set
面试官:算法刷题实录.pdf我居然答不上来
Goweb开发之Beego框架实战:第四节 数据库配置及连接
Azure Devops developed by visual studio 2015 team