helpermethod
helpermethod

Reputation: 62135

Valgrind used in C++ development?

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 like C? Or are there even better tools to use in conjunction with C++?

Upvotes: 7

Views: 2313

Answers (7)

Konrad Rudolph
Konrad Rudolph

Reputation: 545478

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).

Upvotes: 11

pm100
pm100

Reputation: 50100

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

GLIBCPP_FORCE_NEW=1

Upvotes: 2

justin
justin

Reputation: 104698

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.

Upvotes: 2

David Thornley
David Thornley

Reputation: 57036

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.

Upvotes: 5

Šimon Tóth
Šimon Tóth

Reputation: 36423

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.

Upvotes: 1

Sam Miller
Sam Miller

Reputation: 24164

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.

Upvotes: 2

Koteswara sarma
Koteswara sarma

Reputation: 434

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

Upvotes: 4

Related Questions