There are signed and unsigned comparisons pit

Foreword

Often encounter "comp.c: 59: 42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]" in the project is compiled c / c ++ when this error. As a "qualified programmers" to compile such a warning, the usual treatment is ignored, after all, the unanimous view is: only "warning" not a problem!

Here are a small case:

#include <stdio.h>

int sum_elements(int a[], unsigned int length)
{
        int i;
        int result = 0;
        for (i = 0; i <= length-1; i++)
                result += a[i];
        return result;
}

int main(int argc, char *argv[])
{
        int a[] = {1, 2, 3};
        int m = sum_elements(a, 0);
        printf("%d\n", m);
        return 0;
}

 All the way hundreds of times, compile and run, but got a segmentation fault. . .

root@HF-LEE:/mnt/c/Users/Q/Desktop# g++ -Wall -g aaa.c -o aaa
aaa.c: In function ‘int sum_elements(int*, unsigned int)’:
aaa.c:7:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                             for (i = 0; i <= length-1; i++)
                                         ~~^~~~~~~~~~~
root@HF-LEE:/mnt/c/Users/Q/Desktop# ./aaa
Segmentation fault (core dumped)
root@HF-LEE:/mnt/c/Users/Q/Desktop#

 Here's what happened then, look at the code is only one array, it must be cross-border! ! ! (Analysis Reference: https: //www.cnblogs.com/idorax/p/6881996.html) veterans will be here plus cast to solve the problem. But the issue is resolved, then the next, next time there is still have this problem! So we need to understand the nature of the next question, and how to avoid it.

Know almost followed in the common saying: "Is not ask, asking why?"

Unsigned problems caused by:

  As shown in the previous example, unsigned and there is a problem caused when comparing the number of symbols is the most easily overlooked, but also may be the most deadly. If unsigned appeared cycle could cause an infinite loop, an array of unsigned appear may result array bounds. It certainly can not blame unsigned, more should be said that there are problems caused by the number sign mindset caused.

  Unsigned problem is usually caused when comparing values ​​will reflect overflow and unsigned. Are the default type conversion boundary condition detect an incorrect result.

  Since unsigned be a problem, so we can not abandon it? ? ? You can practice using the set of java, never use unsigned on customs (java have this program), but you will have problems dictation particular scene: such as network programming, the serial read and write. . .

How rational use of unsigned:

  This is the core of the problem! Here is my summary of some of the content

  1 bit arithmetic , modulo operation, using the overflow wrapping algorithm in more (such as various cryptography algorithms, coding, compression algorithm, etc.)

There are bits during the operation when the sign bit of the number of symbols will cause some confusion, bit computing when if unsigned will greatly reduce dealing Reflection on language, can concentrate more attention to practical problems.

  2. unsigned numbers in a network transceiver, when the serial read and write

TCP / IP often encounter unsigned, such as IP representation, we can use ip2long the dotted decimal ip turn into a unsigned int to represent, this will bring great convenience. More streams serial read and write is to use unsigned char, the most common problem is unsigned prefix '0xff' confuse people signed char output due time in accordance with the log output can be avoided.

  3 to avoid direct contact with the non-signed numbers of symbols, including comparison operation

When comparing unsigned number and a signed number, the compiler will issue a warning. At the same time there is an internal compiler default type conversion rules (compiler automatically, without user perception). Roughly divided into three categories (please correct any errors) (Note: in the computer, using the anti-code representation of negative numbers)

  First top rules: Signed (int), unsigned (unsigned int), non unsigned (except int and unsigned int types, such as char, unsigned char), a non-signed (previously empathy).

  Signed and unsigned comparison: converting signed numbers into unsigned numbers will be compared (e.g., int compared unsigned int, int converted to unsigned int).

  Signed Unsigned compared to the number of non-: non-signed to unsigned conversion (e.g. comparison with int unsigned char, unsigned char converted int).

  Unsigned and signed a relatively non-: Signed converted into a non-signed (e.g., compared to unsigned int char, char is converted to unsigned int).

  4. Do not just because of a number can not be negative on the use of unsigned

Because this may seem very similar requirements, but the problem is when an unsigned overflow when brought very likely to be fatal.

Guess you like

Origin www.cnblogs.com/sinpo828/p/10943157.html