./beam-3.4.2/bin/beam_configure --c gcc ./beam-3.4.2/bin/beam_configure --cpp g++ ./beam-3.4.2/bin/beam_compile --beam::compiler=compiler_cpp_config.tcl -cpp code2.cpp
从下面的编译报告中,我们可以看到这段程序中有三个错误:”内存泄漏”;“变量未初始化”;“ 空指针操作”
"code2.cpp", line 10: warning: variable "b" was set but never used int b, c; ^ BEAM_VERSION=3.4.2 BEAM_ROOT=/home/hanzb/memdetect BEAM_DIRECTORY_WRITE_INNOCENTS= BEAM_DIRECTORY_WRITE_ERRORS= -- ERROR23(heap_memory) /*memory leak*/ >>>ERROR23_LeakTest_7b00071dc5cbb458 "code2.cpp", line 24: memory leak ONE POSSIBLE PATH LEADING TO THE ERROR: "code2.cpp", line 22: allocating using `operator new[]' (this memory will not be freed) "code2.cpp", line 22: assigning into `Logmsg' "code2.cpp", line 24: deallocating `Logmsg' because exiting its scope (losing last pointer to the memory) -- ERROR1 /*uninitialized*/ >>>ERROR1_foo_60c7889b2b608 "code2.cpp", line 16: uninitialized `c' ONE POSSIBLE PATH LEADING TO THE ERROR: "code2.cpp", line 10: allocating `c' "code2.cpp", line 13: the if-condition is false "code2.cpp", line 16: getting the value of `c' VALUES AT THE END OF THE PATH: p != 0 -- ERROR2 /*operating on NULL*/ >>>ERROR2_foo_af57809a2b615 "code2.cpp", line 17: invalid operation involving NULL pointer ONE POSSIBLE PATH LEADING TO THE ERROR: "code2.cpp", line 13: the if-condition is true (used as evidence that error is possible) "code2.cpp", line 16: the if-condition is true "code2.cpp", line 17: invalid operation `[]' involving NULL pointer `p' VALUES AT THE END OF THE PATH: c = 1 p = 0 a <= 0
3. 动态运行检测
实时检测工具主要有 valgrind, Rational purify 等。
3.1 Valgrind