C++ - detect out-of-range access

大兔子大兔子 提交于 2019-12-21 06:07:49

问题


I would like to analyse my C++ code to find bad access in vectors and arrays (out of range access), is there a tool for that ?

thanks in advance


回答1:


You can compile with _GLIBCXX_DEBUG and _GLIBXX_DEBUG_PEDANTIC defined if your are using gcc. This will enable glibc assertions to be raised if out of bounds access is requested.

Testing with valgrind::memcheck will also reveal bad memory reads




回答2:


Yes: Valgrind.

And many others: http://en.wikipedia.org/wiki/Memory_debugger.

Note also that most implementations of e.g. std::vector have a debug mode where they will do run-time bounds checking.




回答3:


There are some paid tools, which does static analysis:

klockwork

coverity

IBM Rational Purify does dynamic analysis.




回答4:


create a class on you arrays and create some controlling functions in that class with operators and .... then use this class .



来源:https://stackoverflow.com/questions/7497637/c-detect-out-of-range-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!