Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:49:08+00:00 2026-05-11T01:49:08+00:00

How can I track the memory allocations in C++, especially those done by new

  • 0

How can I track the memory allocations in C++, especially those done by new/delete. For an object, I can easily override the operator new, but I’m not sure how to globally override all allocations so they go through my custom new/delete. This should be not a big problem, but I’m not sure how this is supposed to be done (#define new MY_NEW?).

As soon as this works, I would assume it’s enough to have a map somewhere of pointer/location of the allocation, so I can keep track of all allocations which are currently ‘active’ and – at the end of the application – check for allocations which have not been freed.

Well, this seems again like something that surely has been done several times at least, so any good library out there (preferably a portable one)?

Related Questions

No related questions found

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:49:08+00:00Added an answer on May 11, 2026 at 1:49 am

    I would recommend you to use valgrind for linux. It will catch not freed memory, among other bugs like writing to unallocated memory. Another option is mudflap, which tells you about not freed memory too. Use -fmudflap -lmudflap options with gcc, then start your program with MUDFLAP_OPTIONS=-print-leaks ./my_program.

    Here’s some very simple code. It’s not suitable for sophisticated tracking, but intended to show you how you would do it in principle, if you were to implement it yourself. Something like this (left out stuff calling the registered new_handler and other details).

    template<typename T> struct track_alloc : std::allocator<T> {     typedef typename std::allocator<T>::pointer pointer;     typedef typename std::allocator<T>::size_type size_type;      template<typename U>     struct rebind {         typedef track_alloc<U> other;     };      track_alloc() {}      template<typename U>     track_alloc(track_alloc<U> const& u)         :std::allocator<T>(u) {}      pointer allocate(size_type size,                       std::allocator<void>::const_pointer = 0) {         void * p = std::malloc(size * sizeof(T));         if(p == 0) {             throw std::bad_alloc();         }         return static_cast<pointer>(p);     }      void deallocate(pointer p, size_type) {         std::free(p);     } };  typedef std::map< void*, std::size_t, std::less<void*>,                    track_alloc< std::pair<void* const, std::size_t> > > track_type;  struct track_printer {     track_type * track;     track_printer(track_type * track):track(track) {}     ~track_printer() {         track_type::const_iterator it = track->begin();         while(it != track->end()) {             std::cerr << 'TRACK: leaked at ' << it->first << ', '                       << it->second << ' bytes\n';             ++it;         }     } };  track_type * get_map() {     // don't use normal new to avoid infinite recursion.     static track_type * track = new (std::malloc(sizeof *track))          track_type;     static track_printer printer(track);     return track; }  void * operator new(std::size_t size) throw(std::bad_alloc) {     // we are required to return non-null     void * mem = std::malloc(size == 0 ? 1 : size);     if(mem == 0) {         throw std::bad_alloc();     }     (*get_map())[mem] = size;     return mem; }  void operator delete(void * mem) throw() {     if(get_map()->erase(mem) == 0) {         // this indicates a serious bug         std::cerr << 'bug: memory at '                    << mem << ' wasn't allocated by us\n';     }     std::free(mem); }  int main() {     std::string *s = new std::string;         // will print something like: TRACK: leaked at 0x9564008, 4 bytes } 

    We have to use our own allocator for our map, because the standard one will use our overridden operator new, which would result in an infinite recursion.

    Make sure if you override operator new, you use the map to register your allocations. Deleting memory allocated by placement forms of new will use that delete operator too, so it can become tricky if some code you don’t know has overloaded operator new not using your map, because operator delete will tell you that it wasn’t allocated and use std::free to free the memory.

    Also note, as Pax pointed out for his solution too, this will only show leaks that are caused by code using our own defined operator new/delete. So if you want to use them, put their declaration in a header, and include it in all files that should be watched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer try this : width :args.width * 1.0 ||200, height :args.height… May 11, 2026 at 8:41 am
  • added an answer VBA is an old method of building Office plug-ins and… May 11, 2026 at 8:41 am
  • added an answer I will assume that you have your own reason for… May 11, 2026 at 8:41 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.