1: Wrong use is not self-knowledge
2: Wrong export
3: Error resolution
4: Understanding and perception after solution
1: I believe everyone has heard of it, and I know it, and I also know the harm. But I don't know what I've been using is a wild pointer. I'm ashamed to say.
Use pointer before
//Function: read the contents in the pointer //Implementation: output the contents of the pointer to the serial port assistant //Author: maosql //Time: September 9, 2021 void DisplayPointer(void) { char *p; //u8 *p; const u8* zfc="masoql"; //Connection password sprintf((char*)p,"AT+XXX\"%s\"",zfc);//Set wireless parameters: ssid, password }
I take it for granted that this string is stored in the pointer p: the first address is stored later in turn. When I visit, the value in it should be the value I saved. In fact, it should be like this, but there are many uncertainties, because such use is a wrong operation, and the pointer cannot be assigned in this way;
Error: the pointer cannot be assigned before allocating memory space. If the memory behind p is used, the string cannot be assigned, and the program crashes
Consequences: this procedure will appear. It may still be good now. After a day, I didn't do anything and may not be able to run
2: Although it is very harmful, I don't know that there are no problems with such use before. There may be problems, and I don't think about it in this direction. When one day I need to access and output the content inside, I find that I can't access it. At first, I thought that the printf output pointer was in the wrong format. I Baidu that the output method others said was still wrong, and then I have been able to output it by chance, but I can't understand it
1. The correct printf output pointer format, but the read content is the h port (God. What's this!! see the screenshot below)
//Function: read the contents in the pointer //Implementation: output the contents of the pointer to the serial port assistant //Author: maosql //Time: September 9, 2021 void DisplayPointer(void) { char *p; //u8 *p; const u8* zfc="masoql"; //Simple string (??? Why can I assign a value again ↓↓) /* eg:Like char *p = "hello"; This is not to define a pointer p and assign a value to it, and the contents are filled with Hello. Instead, a string like hello exists in a place in memory, and then defines a pointer to point here */ sprintf((char*)p,"AT+XXX,\"%s\"",zfc);//Store at + XXX and "maosql" in the memory with p as 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); }
Results: (the code format is the same. The original code is not used in the article for convenience)
2. The same output format as above is to add a sentence after it to display the desired result (why, the following statements can also affect the previous statements? See the screenshot below)
//Function: read the contents in the pointer //Implementation: output the contents of the pointer to the serial port assistant //Author: maosql //Time: September 9, 2021 void DisplayPointer(void) { char *p; //u8 *p; const u8* zfc="masoql"; //Connection 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: the result can be correctly output, but why? Why can the result be correctly output after adding this sentence, which is unreasonable? Until now, I haven't understood why. If a big man understands, please comment on the guidance of the area. (but I realized that the wrong starting point and strange problems occurred, so I directly learned how to use it correctly)
3: Abandon the error problem caused by the above incorrect use and directly use the method of correctly assigning a value to the pointer
Method 1:
//Function: read the contents in the pointer //Implementation: output the contents of the pointer to the serial port assistant //Author: maosql //Time: September 9, 2021 void DisplayPointer(void) { char p[64] = {0};//The method of declaring array is to realize the function of pointer, and the use of pointer is realized through operation p. as we all know, array and function itself are the first address of pointer const u8* zfc="masoql"; //Simple string (??? Why can I assign a value again ↓↓) /* eg:Like char *p = "hello"; This is not to define a pointer p and assign a value to it, and the contents are filled with Hello. Instead, a string like hello exists in a place in memory, and then defines a pointer to point here */ sprintf((char*)p,"AT+XXX\"%s\"",zfc); //Store at + XXX and "maosql" in the memory with p as the first address printf("%s\r\n",(char *)p); // || printf(p); || printf("%s\r\n",p);// Format can output content correctly }
Method 2:
//Function: read the contents in the pointer //Implementation: output the contents of the pointer to the serial port assistant //Author: maosql //Time: September 9, 2021 void DisplayPointer(void) { char *p; //u8 *p; const u8* zfc="masoql"; //Simple string (??? Why can I assign a value again ↓↓) /* eg:Like char *p = "hello"; This is not to define a pointer p and assign a value to it, and the contents are filled with Hello. Instead, a string like hello exists in a place in memory, and then defines a pointer to point here */ p=mymalloc(SRAMIN,32); //Request 32 bytes of memory sprintf((char*)p,"AT+XXX\"%s\"",zfc); //Store at + XXX and "maosql" in the memory with p as 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: the output is correct without any strange problems
4: Understanding and perception
The pointer points to an address, always for the convenience of reading the contents of the existing address. If you want to point to the address value of the existing memory address, if you want to assign a value to the pointer, you can use the above two methods. One is used as an array to dynamically apply for memory space (remember to release and set NULL).