Menu

idea for a new checker: if (v.size() > v.capacity())

2015-11-17
2016-10-07
  • Dominique Pelle

    Dominique Pelle - 2015-11-17

    I found a piece of c++ code which was obviously incorrect, and I thought that it could be an idea for a new checker:

    std::vector<...> v;
    ...
    if (v.size() > v.capacity()) {
    ...
    }

    The 'if' condition is always false since v.size() can never be bigger than v.capacity(). So the code is incorrect. The code was actually meant to do...

    if (v.capacity() > v.size()) {
    ...
    }

    cppcheck could probably warn about such a condition which is always false (size of vector never exceeds capacity). gcc or clang do not warn about this. Latest cppcheck in git does not warn about it either.

    PS: I wanted to open a trac ticket about this, but I forgot my password and I see no way of requesting a new password.

     
  • Daniel Marjamäki

    I can give you a new password. what username do you have?

     
  • Dominique Pelle

    Dominique Pelle - 2016-10-06

    I can give you a new password. what username do you have?

    I'm replying very late. I'm fine after all, if reporting suggestions for cppcheck
    is allowed in the forum. I did not see any comments about the possible
    improvement reported in this thread.

     
  • Mr. X

    Mr. X - 2016-10-07

    It is allowed, but it is very likely that the information gets lost if it is not in the bugtracker.

     

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.