esp8266 sprintf function use overflow problem

When using ESP8266 to run the freertos operating system, if you use the sprintf function to convert other data into a string format, if you define the array storing the string as a local array, the program will die and then restart. In order to avoid the problem of program running to death, it is necessary to define the storage string array as a global array or a local array and declare it with the static keyword. The reason for this problem is that there is a problem with the address offset of the array when the thread is switched, and there is a problem with switching addresses back and forth. Under normal circumstances, we will not have this problem when we define it as a global variable to prevent stack overflow.

Another reason for the restart is that the definition of the array storing the string data is too small, and the return value of the sprintf function can be used to judge how much space needs to be opened up for the array.

Here is an example:

#define BUFF_SIZE         9

uint8 signall[BUFF_SIZE]; //Defined as a global variable

len = sprintf(signall,"%d,  %d", idx++, wifi_signal);    

Guess you like

Origin blog.csdn.net/qq_27568125/article/details/54912424