"C-language" integer overflow and integer boost

Source: Internet
Author: User

What is an integer overflow:

The problem of the integral type of C language is not unfamiliar to everyone. For an integer overflow, an unsigned integer overflow and a signed integer overflow are classified.

for a unsigned integer overflow, the specification of C is defined --"The number after overflow is modeled with 2^ (8*sizeof (Type)", that is, if a unsigned char (1 characters, 8bits) overflows, The value of the overflow will be modeled with 256. For example:

unsigned char x = 0xFF; printf ("%d\n", ++x);

The above code will output: 0 (because 0xFF + 1 is 256, and 2^8 is 0 after modulo)

for signed integer overflow, the canonical definition of C is "undefined behavior", that is, how the compiler loves to implement it. For most compilers, that's what it is. Like what:


Signed Char x =0x7f; Note: 0xFF is-1, because the highest bit is 1 is the negative number of printf ("%d\n", ++x);

The above code will output:-128, because 0x7f + 0x01 get 0x80, that is, the binary 1000 0000, the sign bit is 1, negative number, followed by all 0, is the smallest negative, that is-128.

In addition, do not think signed integer overflow is negative, this is uncertain. Like what:

Signed char x = 0x7f; Signed char y = 0x05; Signed char r = x * y; printf ("%d\n", R);

The above code will output: 123

I believe that we will not be unfamiliar with these people. 、


What is an integral type of promotion:


Most c programmers think that the basic operation of an integral type is safe. In fact, the basic operation of the integral type is also prone to problems, such as the following code:

int main (int argc, char** argv) {    Long i =-1;    if (I < sizeof (i)) {         printf ("ok\n");    }    else {         printf ("error\n");    }    return 0;}
in the above code, the variable Iare converted to unsigned integers. In this way, its value is no longer-1, but size_tthe maximum value. Variables Iis converted because the type of sizeofThe return type of the operator is unsigned.


Let's look at the following code:

#include <stdio.h>int array[] = {1, 2, 3, 4, 5, 6, 7}, #define Total_elements (sizeof (array)/sizeof (array[0])) int m Ain () {    int i =-1;    int x;    if (i <= total_elements-2) {        x = array[i + 1];        printf ("x =%d.\n", x);    }    printf ("Now i =%d.\n", total_elements);    return 0;}

Execution Result:

[Email protected]:~/c_language$./a.out

Now i = 7.

Isn't it weird? Why not line13 x =?.

That is true. This small example has three points worth noting:

1.sizeof () is an operator that returns a type that is unsigned, that is, a non-negative number.

The 2.if statements are judged between singned int and unsigned int, and the value of the original type is converted to the int type if all values of the primitive types are represented by the int type, otherwise it is converted to the unsigned int type. so I will be upgraded to an unsigned type at this point.

3.i = 1 is promoted to unsigned, what is the value? This is done using an integer conversion rule: K&R, the method of converting any integer to a specified number of unsigned number types is to find the smallest non-negative value that is equal to this integer by adding 1 to the maximum that the unsigned number type can represent. Listen to very awkward, in fact, as long as the original integer to know the binary expression method, and then the type to be converted to parse, you get the value of the upgrade. For example-1, negative numbers in the computer with a complement, 0xFFFFFFFF, that the upgrade to unsigned after the value is 0xFFFFFFFF, obviously larger than total_elements (7) .


In K&R C, the definition of integral type Promotion (integral promotion) is:

"A character, a short integer, or an integer bit-field, any either signed or not, or an object of enumeration type, could be Used in a expression wherever an integer maybe used. If a int can represent all the values of the original type and then the value is converted to int; Otherwise the value is converted to unsigned int. This process is called integral promotion. "


The above definition is summed up in the following two principles:

1). Whenever an expression is used with an integer value, a variable of type char, short int, or an integer bit field (which is either signed or unsigned), and an object of the enumerated type, can be placed in the position of the integer variable.


2). If the original type value of the variable in 1 can be represented by an int, then the original is converted to int; otherwise, it becomes unsigned int.

The above two as a whole, are becoming integral type lifting (Integral promotion)

The concept of integral lifting is easily confused with ordinary arithmetic type conversions. One of the differences between the two is that the latter occurs when the type is inconsistent between operands, and ultimately converts the operand to the same type. In the case of arithmetic operations, an integral promotion can occur even if the operands have the same type.

For example:

Char A, B, C;

c = a + B;

In the above procedure, although the operands of the two operators "+" and "=" are all char types, there is an integer promotion in the intermediate calculation: for the expression a+b, a, B are char, and therefore are promoted to the int type, the "+" operation is performed, and the computed result (int) is then assigned to C ( char), and an implicit type conversion is performed.


sizeof (A+B)------> value is 4

There is a small problem to remind you:

Integer lifting occurs only when the arithmetic operation,sizeof (' a ') should not have an integer promotion, but in C ' a ' is the int type, sizeof (' a ') = 4, whereas in C + + sizeof (' a ') = 1.


Finally add two small eggs, you can deepen understanding (must see);

Http://www.360doc.com/content/12/1129/15/1317564_250976513.shtml

Http://www.360doc.com/content/12/1129/15/1317564_250976830.shtml

"C-language" integer overflow and integer boost

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.