Menu

2.6 FP on static variable?

jacob s
2021-10-06
2021-10-07
  • jacob s

    jacob s - 2021-10-06
    int get_delta() {
        clock_t now_ms = (clock() / (CLOCKS_PER_SEC / 1000));
        static clock_t last_clock_ms = now_ms;
        clock_t delta = now_ms - last_clock_ms;
        last_clock_ms = now_ms;
    
        if (delta > 50)
            delta = 50;
        return delta;
    }
    

    gives

    fp.c:7:15: style: Condition 'delta>50' is always false [knownConditionTrueFalse]
        if (delta > 50)
                  ^
    fp.c:3:34: note: last_clock_ms is assigned 'now_ms' here.
        static clock_t last_clock_ms = now_ms;
                                     ^
    fp.c:4:28: note: Assignment 'delta=now_ms-last_clock_ms', assigned value is 0
        clock_t delta = now_ms - last_clock_ms;
                               ^
    fp.c:7:15: note: Condition 'delta>50' is always false
        if (delta > 50)
    

    which is only true for the first call. this warning was not thrown with 2.5.

     
  • CHR

    CHR - 2021-10-07

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/10534

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.