Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: "Watch" usage of "new", "delete" operators

I'd like to track down when and how much memory gets allocated in my program and print it out for debugging purposes under certain circumstances!

  • How can I print out a message with the allocated memory amount every time new is used to allocate memory in my program?
like image 868
Jarx Avatar asked Jan 26 '26 20:01

Jarx


2 Answers

An excellent way to debug memory problems is to use an external monitor such as valgrind. This will hook into the memory allocation and deallocation of your program, and print out a report at the end of your program showing any allocations that you didn't deallocate. No modification or recompilation of your program is necessary with this method.

like image 199
Greg Hewgill Avatar answered Jan 29 '26 10:01

Greg Hewgill


You could overload the new and delete operators. See this article.

like image 34
HighCommander4 Avatar answered Jan 29 '26 10:01

HighCommander4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!