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

  • SEARCH
  • Home
  • 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 7187329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:49:15+00:00 2026-05-28T18:49:15+00:00

If we would like to check for memory leaks in a C++ program, we

  • 0

If we would like to check for memory leaks in a C++ program, we can overload the new and delete operators to keep track of the memory that was allocated. What if we would like to check for leaks in a C program? Since there is no operator overloading in C, can we over-write the malloc function pointer to intercept calls to malloc and track memory allocation? Is there an easier way without using any external utilities? Please provide some code as I am not familiar with over-writing method pointers.

Note: I would like to do this without any external utilities for practice.

  • 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. Editorial Team
    Editorial Team
    2026-05-28T18:49:16+00:00Added an answer on May 28, 2026 at 6:49 pm

    As suggested, there already exist excellent tools like Valgrind to do this.

    Further:

    I would like to do this without any external utilities for practice
    This is interesting and I am sure would be fulfilling,
    You can use macro trick to detect such memory usage and leak errors, in fact write your own neat leak detector. You should be able to do this as long as you have a single allocation and deallocation function in your project.

    #define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)
    
    void* my_malloc(size_t size, const char *file, int line, const char *func)
    {
    
        void *p = malloc(size);
        printf ("Allocated = %s, %i, %s, %p[%li]\n", file, line, func, p, size);
    
        /*Link List functionality goes in here*/
    
        return p;
    }
    

    You maintain a Linked List of addresses being allocated with the file and line number from where there allocated. You update the link list with entries in your malloc.

    Similar to above you can write an implementation for free, wherein you check the address entries being asked to be freed against your linked list. If there is no matching entry its a usage error and you can flag it so.

    At the end of your program you print or write the contents of your linked list to an logfile. If there are no leaks your linked list should have no entries but if there are some leaks then the logfile gives you exact location of where the memory was allocated.

    Note that in using this macro trick, you lose the type checking which functions offer but it’s a neat little trick I use a lot of times.

    Hope this helps and All the Best 🙂

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

Sidebar

Related Questions

I would like to check the memory consumption of each running processes individually, cat
I have a WiX installer that I would like to check for .Net 3.5,
Can we check whether a pointer passed to a function is allocated with memory
I would like to check which type of RAM my computer uses before I
I would like to check my JavaScript files without going to JSLint web site.
I would like to check whether an object (e.g. someObject ) is assignable (cast-able)
I would like to check if a few URL's exst on my old website
I would like to check whether the request is XML od HTML. When HTML
I would like to check if an object is defined or exists using C#.
I would like to check what colors is present in a image. This will

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.