当前位置:网站首页>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).
边栏推荐
- C#基础面试题(附答案)
- Create a custom paging control
- 部分情况下Error:(xx, xx) Failed to resolve: xxxxxx解决。
- Verilog daily question (vl26 simple stopwatch)
- Linear algebra and matrix theory (VIII)
- Verilog 每日一题(VL29 单端口RAM)
- Verilog 每日一题 (VL5 信号发生器)
- Verilog daily question (vl27 settable counter)
- Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
- Goweb开发之Beego框架实战:第四节 数据库配置及连接
猜你喜欢

MySQL的触发器

一文掌握 JVM 面试要点

Several methods of importing excel file data by C #

深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向

Visual studio 2012/2015 releases web applications together with.Cs source code

Proof of the third scene (f) in 22 years

Basic principle of asynchronous FIFO (simple implementation based on Verilog)

Valarray Library Learning

How to protect image security during construction

influxdb2的使用
随机推荐
js将本地时间与服务器时间同步
[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
【presto 】presto 新版本升级详情
ng-repeat在迭代最后一个元素时执行一个方法
LNMP source code compilation and installation
How do we do full link grayscale on the database?
部分情况下Error:(xx, xx) Failed to resolve: xxxxxx解决。
Self study examination in April 2021
How to protect image security during construction
Goweb开发之Beego框架实战:第四节 数据库配置及连接
MySQL detailed learning tutorial (recommended Collection)
@RequestParam使用
循环中使用switch在执行条件后使用break和continue的区别
Linear algebra and matrix theory (10)
Selection of resistance in high speed circuit
Connection design and test platform -- Summary of SystemVerilog interface knowledge points
Proof of the third scene (f) in 22 years
Why do I choose to use go language?
Verilog 每日一题(VL26 简易秒表)
Soft exam review summary