1. assume that you want to receive a lot of (or even unlimited) data, but the data has a range: for example, if you want to read the data sent by the serial port, the data range is 0x00-0xff.
2. If an array or container is directly defined for receiving, unlimited memory is required. This method is not feasible.
3. an ingenious method is to define an array (data [0xff + 1]) that covers the size of the required data range, but when receiving a data, it is subscript, assign values to the corresponding position of the array (for example, if the received data is 0x0a, the value of data [0x0a] = 0 ), the second data received is 1 (data [0x1c] = 1), and so on. Each received data has its own value.
4. Then define an array showdata [500] for displaying data, and use the value of data [] as the subscript. In this case, when receiving data, store the data to showdata [0] with its data [] value as a subscript, the next received data is sequentially placed in showdata [I ++. (For example, if 3 knows its data [0x0a] = 0 after receiving data from 0x0a, the data is saved to showdata [0 ····, assume that after receiving data for several times, 0x0a data is received again. The data [0x0a] = 0, therefore, the data content is stored in showdata [0], and the previous data is overwritten. Therefore, a finite array can receive and display infinite data ).
This article is from the "whatever957" blog, please be sure to keep this source http://whatever957.blog.51cto.com/6835003/1560759
A clever way to receive infinite data within a limited range