The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648

Today, I encountered the need to use qsort function in the force deduction problem. As a result, there was no test case. The error reports are as follows

signed integer overflow: 0 – -2147483648 cannot be represented in type ‘int

Signed integer overflow: 0 — 2147483648 cannot be represented in type ‘Int’

The error was reported in the CMP function. At that time, I wondered if it was still within the range. I thought about it and found the problem

This is my original CMP function  

int cmp(const void *a,const void *b){
    return (*(const int*)a > *(const int*)b);
}

At this time, a is – 2147483648. Subtract B. as long as B is greater than 0, it overflows

terms of settlement:

Change the minus sign to the greater than sign

int cmp(const void *a,const void *b){
    return (*(const int*)a > *(const int*)b);
}

So it passed smoothly.

There are few questions about this on the Internet. Maybe it’s too stupid. Send a post to help Xiaobai like me

Read More: