Csapp: Unsigned number of possible program bugs

Source: Internet
Author: User

From Csapp Exercise 2.26

size_t strlen (constChar *s); int strloner (char *s,char *t) {    return strlen (s)-strlen (t);}

At first glance there is no problem, but size_t is defined as unsigned int, then when the s string length is less than t string, the result is negative, for the unsigned number is both a large unsigned number, so that the result is 1, the result is wrong

Correction can be calculated by avoiding the unsigned number of the following code, suitable for the calculation of length difference

int strlonger (char *s,char *t) {    return (int) strlen (s)-( int) strlen (t) >0;}

Of course, this problem as long as the length, can be changed to the following code, do not introduce calculations on the line

int strlonger (char *s,char *t) {    return strlen (s) > strlen (t);}

Csapp: Unsigned number of possible program bugs

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.