A bug that crosses an array at a time

Source: Internet
Author: User
Tags array definition sprintf

Arrays and pointers are good things in C, but when used improperly, it can really make people crazy.

Here is the experience of an array that was encountered when writing the program, and it is a bit enlightening to write the program later, so record it.

Cause:

I want to use OLED to dynamically display a set of floating-point numbers, and the length of the floating-point number is variable.

1, if only a simple display, no blanking, the last long number of the residue will affect the next short-length data display. 2, if the display will be emptied once the display area, the data will always shake, at first thought is I refresh frequency is not enough, so the refresh frequency from 100HZ to 1000HZ, but the effect is the same as before! Then think about it, no matter how much I change the refresh rate, the empty space and the displayed data are the same frequency. 1000hz Displays the data, so is the 1000hz blank. So the jitter is serious. 3, each of the data is taken out to show alone, but this brings the data alignment problem. Unpleasant, not good-looking, abandon it. 4. Use sprintf format to display the data as a string. It is then displayed in the form of an OLED display string.

So the following program is available:

 sprintf (char  *) weight_string, "        Span style= "COLOR: #800000" >%.1f   ", weight); //     formatted as a string                                 Clear_left_num (money_string); //     Oled_show_string (42 , 2  ,weight_string"); sprintf (( char  *) price_string, "    Span style= "COLOR: #800000" >%d   " ,price);    Clear_left_num (money_string); Oled_show_string ( 42 , 4 , Price_ string); 

This program is called in the timer interrupt function. Weight and price are the floating-point numbers I want to show.

Formatted as a string and then displayed. The first two parameters of oled_show_string () are the starting display coordinates of the character.

The Clear_left_num function is as follows:

voidClear_left_num (unsignedChar*num_string) {     while(*num_string! ='.') num_string++; //data after a decimal point is refreshed with a space* (num_string+2) =' '; * (num_string+3) =' '; * (num_string+4) =' ';}

The idea is to use a space to refresh the remaining data behind a decimal point.

But the experimental phenomenon is that after the first row of data is displayed, the second data should be displayed in the second row, but he shows the second row of data after the first row of data!! This means that the second row of data is displayed two times.

Why does it show two times? I wrote it once in my program. 、、、

Analysis:

Since it is a display problem, let's look at the display function first!

/*----------------------------------* * Function Name: oled_show_string** function Description: string displayed at cursor, string can be represented by array, unsigned char string_2[] = {"This is A TEST"};** parameter description: x, y coordinates * CHR: string header address **andrew** date: 2018.1.24-----------------------------------*/voidOled_show_string (U8 x, U8 y, U8 *CHR) {U8 J=0;  while(chr[j]!=' /') {Oled_showchar (x,y,chr[j]); X+=8 ; if(x> -) {x=0; y+=2;}//Auto-wrap WriteJ++; }}

This function will display the entire contents of the array before the end of the array. Because the last trailing sign of the array is ' \ s '

Well, the first line above has been shown, indicating that he may not have encountered an array end identifier.

To view the size of an array definition:

Char weight_string[7] = {0char price_string[3] = {0};

The last end of the original weight_string array was assigned a space by me. Then he will always read the memory data stored behind this array and display it. The so-called "array out of bounds".

Fortunately we just read the display, and did not overwrite this data!

Now that he shows the second row of data, the second row of data is stored in memory behind the array.

To view the compiler-generated map file:

Sure enough, the second array is immediately adjacent to the first array store.

After the first array is read out of bounds, the second array is read.

To this, the problem is resolved.

Summarize:

Be sure to see the inner link of the program. It is difficult to analyze memory, but it is a shortcut to find annoying bugs.

A bug that crosses an array at a time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.