开发者

Valgrind used in C++ development?

开发者 https://www.devze.com 2023-01-22 11:12 出处:网络
I\'m quite new to C++ but have some basic C knowledge. In my past C (university) projects, I used Valgrind to check for memleaks.

I'm quite new to C++ but have some basic C knowledge. In my past C (university) projects, I used Valgrind to check for memleaks.

Now, with C++, is Valgrind a valid tool? Does C++ suffer the same problems concerning memleaks开发者_StackOverflow like C? Or are there even better tools to use in conjunction with C++?


I never use new and delete (or other forms of manual memory management) and I very rarely even use pointers. And I still have to wrestle with memory leaks invalid memory accesses.1 Valgrind is an indispensable tool for me. Even more important than gdb.


1 As Viktor pointed out in a comment, producing memory leaks without manual memory management would be pretty weird (discounting circular references and other special cases).


While C++ has much better memory handling than C, it's still quite possible to mess up. Smart pointers are great, but it's possible to make mistakes with them. That's what valgrind is for.


Valgrind can be used to check memleaks in c++ also

valgrind has so many options which will give you info and you can explore callgrind also.

--Cheers


Memory leaks are a concern to myself as a C++ developer. I assume they are a concern to other developers as well, though I cannot speak for everyone. Valgrind is a fantastic tool in this space and one I really could not live without.


remember to tell gcc runtime to not use its own private memory pool otherwise you will confuse valgrind

GLIBCPP_FORCE_NEW=1


Valgrind is the best tool out there for dealing with memory errors (but do check the other modules beside memcheck).

C-style programming is a valid (and widely used) programming approach in C++, therefore yes, memory problems are still an issue.


yes, it is.

i use dynamic allocation by default in unit tests (with auto pointers, or an idiomatic equivalent), to explicitly check additional for memory errors which valgrind may detect. valgrind, guardmalloc, leaks, etc. can catch many errors before they enter production code.

0

精彩评论

暂无评论...
验证码 换一张
取 消