How to solve the problem of data overflow in C language?

Updated on healthy 2024-03-19
12 answers
  1. Anonymous users2024-02-06

    This is an additive function I wrote that can judge overflow:

    #include

    int uoadd (int a,int b,int *overflow);

    int overflow;

    int main ()

    int a=2147483647-9,b=10,c=0;

    c=uoadd(a,b,&overflow);

    if (overflow == 1)

    printf ("Overflow! ");

    elseprintf ("%d",c);

    return (0);

    int uoadd (int a,int b,int *overflow)

    overflow=0;

    if (a>0 &&b>0 &&a+b<0) *overflow=1;

    if (a<0 &&b<0 &&a+b>0) *overflow=1;

    return (a+b);

    vc6 compilation, select the console program when creating a new project.

    In fact, other applications may have built-in judgment overflow things, such as VB will judge, if the data overflow will pop up a dialog box, and the others will not know.

    Actually, it's fairly easy to solve this thing with assembly, but it's better to use C for readability.

  2. Anonymous users2024-02-05

    In high-level languages, there is no function that directly determines overflow.

    If you write one yourself, use it behind various calculations, and judge whether it is overflowing after each calculation, it is too wasteful of resources.

    From this point of view, assembly language is clearly superior.

  3. Anonymous users2024-02-04

    The materials that need to be prepared are: computer and C language compiler.

    1. First of all, open the C language compiler and create a new initial. cpp file, e.g., Enter the Question Basis**.

    2. In the file, adjust all ints to long long and the printf function to.

    3. The compiler runs the file, and a large number is successfully printed at this time.

  4. Anonymous users2024-02-03

    The power of C lies in its flexibility, which the programmer can grasp for himself. Compilers can help prevent errors, but some programs take advantage of overflows, so overflows aren't exactly a bug thing.

    1. Overflow: Data overflow occurs when the data type exceeds the limit of the computer's word length;

    2. Possible causes of overflow:

    The memory buffer may overflow when the application reads user data and copies it to the memory buffer created by the application, but cannot guarantee that there is enough space in the buffer (assuming that the array int array[10] is defined, and array[11] is used when invoking, or the data stored exceeds the capacity of the int type, etc.).

    3. Analysis of spillover factors:

    Due to the inherent defects of the C C++ language, which does not check the array boundaries and does not check the type reliability, and the program developed in the C C++ language has direct access to memory and registers because the target ** is very close to the machine core, as long as the code is reasonable, the C C++ application will inevitably be better than other high-level languages in terms of execution efficiency. However, the C C++ language is much more likely to cause memory overflow issues.

  5. Anonymous users2024-02-02

    The question is like: What should I do if I have a cold?

    I mean, all you have to do is prevent data overflow. When assigning values to data, keep in mind the upper and lower limits of the data that this type holds, and when using strings, remember to use the last one'\0'Characters are left one character at a time, and so on.

    This should be a natural reaction, but the process needs to be built up slowly, so it's good to write more **.

  6. Anonymous users2024-02-01

    Obviously, for the relatively large A, the number C is a big number, so you can't use the data type that comes with it, and the long integer type is not enough. You need to define the array yourself, and store each number in a unit, but correspondingly, you also have to write the part of addition, subtraction, multiplication and division calculations, if not, you can search for the addition, subtraction, multiplication and division of the array, and you should be able to search for the relevant algorithm.

  7. Anonymous users2024-01-31

    After testing, the program is not wrong. But the input A can't be too big.

    I type in 5 and it turns out to be 114, so I can't enter too many numbers.

  8. Anonymous users2024-01-30

    c is too late to do overflow detection after calling the product operator, but it's not too late to do the detection after calling the sum operator, so you can implement the product operation that can detect overflow by detecting the sum operation result, because a * b is the sum of a b :

    5000000 * 1374389539 equals -(5000000 * 1374389539). In parentheses is the sum of 5,000,000 1374389539.

    I wrap the sum operation that detects overflow in add( ), and then call add( ) repeatedly in multiply( ).

    add( )How to detect overflow?

    If the result of the sum operation overflows, it will cause the numerical surrounding. The overflow leads to a downward wrap, and the underflow leads to an upward wrap.

    Boundary Condition: 1) The slightest overflow is int max + 1: the result is int min.

    2) The most severe overflow is int max + int max: the result is -2.

    3) The slightest underflow is int min - 1: the result is int max.

    4) The most severe underflow is int min - int min: the result is 0.

    Conclusion: (1) All spillover results are less than any one operand.

    2) All underflow results are greater than any one operand.

    Therefore, add( ) can be used to determine the overflow with the result and an arbitrarily selected parameter, and the direction of the overflow can be determined by the unsuccessful parameter.

    add( ) cannot report an overflow with a return value, so the reporting method of strtol( ) is used.

    Unlike strtol( ), add( ) assigns a 0 to errno if there is no overflow.

    multiply( ) is the same as add( ) in this respect.

    Of course, the best way to deal with spills is to be prepared: to understand the scope of your data and to choose the right type of variable.

  9. Anonymous users2024-01-29

    What do you mean by overflow?

    Does the set value exceed the maximum value represented by the data type?

  10. Anonymous users2024-01-28

    1.Overflow: A data overflow that occurs when the data type exceeds the limit of the computer's word length.

    2.Possible causes of overflow:

    When an application reads user data and copies it to a memory buffer created by the application, but there is no guarantee that there is enough space in the buffer.

    Suppose you define an array int

    array[10], while array[11] is used when called

    or the stored data exceeds the int type capacity, etc.), the memory buffer may overflow.

    3.Spillover Factor Analysis:

    Due to the inherent defects of the C C++ language, which does not check the array boundaries and does not check the type reliability, and the program developed in the C C++ language has direct access to memory and registers because the target ** is very close to the machine core, as long as the code is reasonable, the C C++ application will inevitably be better than other high-level languages in terms of execution efficiency. However, the C C++ language is much more likely to cause memory overflow issues.

    4.For stack overflows, the program simply terminates with an error.

    For an array type of boundary overflow out of bounds, sometimes an interrupt error is generated, and sometimes it can still continue to run, but the result is incorrect.

    For a basic type of maximum overflow, there are inconclusive results, and the program is still able to function normally, but the results are incorrect.

  11. Anonymous users2024-01-27

    Although I don't see your C source program, in my programming experience, the cases that cause data overflows are usually as follows:

    1) The array subscript is out of bounds. This is the most common mistake beginners make in the C language! For example, define an array like this:

    int num[10] ;In C, the effective subscript range should be: 0-9, not 1-10!

    If the num[10] variable is referenced in the source program, it will inevitably cause a data overflow. But in Pascal, the effective subscript range of the array is: 1-10, which is the difference between C and Pascal.

    2) Abnormal use of memory variables, resulting in data overflow. For example:

    char * p ;

    p = char *)malloc(10000)*sizeof(char) ;Dynamically allocate memory * for the pointer variable p

    if( p ==null )

    strcpy( p, "this is a test string!"The memory allocation is successful, copy the string into the p variable *

    As for the rest, it is necessary to accumulate experience in the usual programming process.

  12. Anonymous users2024-01-26

    A 2 is equivalent to a <<1, i.e. it is equivalent to a binary right shift of a by one bit.

    If a number is odd, then the lowest bit of the hole system in the binary cavity must be 1, otherwise it is 0. Therefore, the lowest digit of the number a can be obtained by using a%2.

    The idea of the program is actually like this:

    1) If the lowest digit of the number a is 1 (a%2==1), then let num++

    Otherwise, there is no need to add (because the lowest digit is 0).

    Shift a to the right by one bit (a>>1), make the next lowest bit the lowest bit, and repeat (1) until all bits are processed (i.e., a==0).

    Take a 4-digit integer as an example (such as 10), its binary is 1010, the first cycle: 10% 2==0 (the lowest bit of 1010 is 0), num=0, and then move the number a to the right to 0101 (that is, 5).

    The second cycle: Peisun 5% 2==1 (0101 lowest digit 1), num=1, and then move the number a to the right to 0010 (that is, 2).

    The third cycle of the Wood Wither:

    2%2==0 (0010 has the lowest digit of 0), num=1, and then shifts the number a to the right to 0001 (that is, 1).

    Fourth Cycle:

    1%2==1 (the lowest digit of 0001 is 0), num=2, and then the right shift a becomes 0000 (that is, 0).

    Since a==0, the program ends.

Related questions
11 answers2024-03-19

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

8 answers2024-03-19

The semicolon is the sign of the end of the statement, but the semicolon is not used after the loop, if, and subfunctions such as long long a (int b), and everything else is used, but two semicolons cannot be added, although it will not cause an error, but it may affect the result.

10 answers2024-03-19

In the C language, files can be divided into ASCI files and binary files according to the way the data is organized. ASC code. >>>More

11 answers2024-03-19

srand(int) is used to set the seed, and then returns a random value each time rand(). >>>More

6 answers2024-03-19

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More