Thread: How is this a Memory Leak?

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    How is this a Memory Leak?

    In Teach Yourself C++ in 21 Days there is an
    exercise that asks the reader to create a program
    that produces a memory leak. In the answers section
    of the book, it says that this is the right answer:

    Code:
    //Prototype
    int *Function();
    
    //Main 
    int main()
    {
      int *pMem = Function();
      cout<<"The Value of pMem in main is: " <<pMem <<"\n";
      return 0;
    }
    
    //Function
    int *Function()
    {
      int *pMem = new int;
      cout<<"The value of pMem in Function is: "<<pMem <<"\n";
      return pMem;
    }
    Anyway, both functions print the same address.
    How come the book says this is a Memory Leak?
    Why can't I call delete on main's pMem and free
    the memory? They both have the same address...

    I can't seem to understand this. I don't suppose
    there is any chance that the book is wrong, is
    there?

    Anyway, it's exercise 6 on Day 9 if you have the
    book.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "How come the book says this is a Memory Leak?
    Why can't I call delete on main's pMem and free
    the memory?"

    You can call delete on pMem, and in that case, it woulnd't be a memory leak. However, because you didn't delete the memory, then it is a memory leak.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    By dynamically allocating memory for 'int* pMem' with "new int", you placed it out on the 'heap', somewhere.

    In main(), 'int* pMem' is assigned to the 'stack'.

    You return the address (pMem) to main(), which simply re-displays it. Nothing much more.

    You've deleted nothing in your code.

    You may be thinking that main() is a "function" (true) and that variables are destroyed when they go "out of scope" (true, but...)

    Memory for main() is allocated on the 'stack', typically. When you invoke 'new' (malloc in C), you take the 'stack' out of play and begin "toying" with the 'heap'.

    In short, you've left 'pMem' occupying a memory location on the 'heap' and collected a memory leak by not de-allocating it.

    (Well, you didn't, but the program did!)

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    So, if I just added "delete pMem" to main,
    everything would be okay?

    I thought there was some deeper reason than that.
    The book was talking about things like functions
    returning references to variables that go out
    of scope, and how that's a bad thing... It got
    kind of confusing.

    The only thing wrong with this is that delete
    wasn't called on main's pMem variable?
    And that's the only reason why there is a
    memory leak?

    Thanks a lot.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    de-allocate memory

    ethic,

    Code:
    //Prototype
    int *Function();
    
    //Main
    int main()
    {
      int *pMem = Function();
      std::cout<<"\nThe Value of pMem in main is: " <<pMem <<"\n";
      delete pMem;
      pMem = NULL;
      getch();
      return 0;
    }
    
    //Function
    int *Function()
    {
      int *pMem = new int;
      std::cout<<"The value of pMem in Function is: "<<pMem <<"\n";
      return pMem;
    }
    Deleting pMem deallocates its memory on the 'heap' but doesn't change the pointer value. You also need to set pMem to NULL so that you don't inadvertently use it again (you wouldn't in this particular case, but...).

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM