C++/clang analyzer memory leaks?

2019-06-04 09:56发布

问题:

I'm trying to get clang++ to tell me there is a memory leak. I tried scan-build but it reported nothing. How do I get llvm/clang to warn me of this problem?

#include <iostream>

int main() {
        int *a = new int;
        *a = 8;
        std::cout<< a << std::endl;
}

回答1:

False-positive pruning usually leads to removing all leaks that originate from main(), since the program will exit anyway. Try analyzing the same code, but in a different function.



回答2:

Because int is too small, there is something like one "reserved section" for program so for small object no need to extend memory area, try to apply 1024 * 1024 * 10 then check the result