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 8891023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:03+00:00 2026-06-14T22:38:03+00:00

The following code has an obvious memory leak: void Memory_Leak(void); void Lots_Of_Other_Stuff(void); int main(){

  • 0

The following code has an obvious memory leak:

void Memory_Leak(void);
void Lots_Of_Other_Stuff(void);

int main(){

    Memory_Leak();
    Lots_Of_Other_Stuff();
}

void Memory_Leak(void)
{
  int *data = new int;
  *data = 15;
  return;
}
void Lots_Of_Other_Stuff(void){
    //allocates/deletes more memory
    //calls functions
    //etc..
    return;
}

For the duration of the program, can the memory ever be recovered?
Can the program write over lost memory, and reach a state where no memory has been lost?
Can the Operating System recover it while the program is still running?

  • 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-06-14T22:38:05+00:00Added an answer on June 14, 2026 at 10:38 pm

    Standard C++ does not have any way to know you’re not using the memory anymore.

    Some platform-specific mechanisms exist for introspecting the memory heap, usually for debugging, e.g.

    http://msdn.microsoft.com/en-us/library/974tc9t1(v=vs.80).aspx

    In theory, you could maybe use something like that to take a “snapshot” of the heap state before you ran your Memory_Leak(). Then after it was finished you could look for anything you considered to be a leak and free it. But don’t do it. Only mentioning it for thoroughness.

    The C++ way of avoiding leaks is to use “smart” pointers instead of “raw/naked/dumb/C-style” pointers. For instance:

    void Memory_Leak(void) // actually, with this change it won't leak anymore...
    {
        shared_ptr<int> data (new int);
        *data = 15;
        return;
    }
    

    The shared pointer is an object with a destructor, so it has an opportunity to run some code when its lifetime is over. That code releases the memory. In this case, the local variable data ends its life at the return statement, and if that shared_ptr hasn’t been copied and stored elsewhere then the reference count held on the memory for the integer will be zero. So that memory will be freed.

    You can read up more on smart pointers here on StackOverflow, Wikipedia, Google, etc.

    http://en.wikipedia.org/wiki/Smart_pointer

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

Sidebar

Related Questions

The following code has concurrency issues. Here inDegVec is a global int * ,
No doubt this will be something obvious, but the following code has 2 errors
So I'm trying to play a video and the following code has worked for
I am generating a simple form with php. The following code has been reduced
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
Class 1 has the following code that generates the exception - -(IBAction) searchAllAction: (id)
The documentation has the following code var options = { host: 'www.google.com', port: 80,
The following code, d has the current date. Depending on the current locale, it
The following code tries to attach to an excel session that has the C:\test

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.