Menu

Undetected always true condition

Fraser
2026-08-21
2026-08-27
  • Fraser

    Fraser - 2026-08-21

    With the following code when there is failure to allocate memory std::bad_alloc is thrown. The following test of the pointer is useless. This is from C++ Gotchas P172. I think it would be worthwhile to catch this with CPPCheck.

    int * ip = new int;
    if (ip)
    std::cout << "Always executed";
    else
    std::cout << "Never executed";

     
  • CHR

    CHR - 2026-08-27

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

     

Log in to post a comment.