CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    8

    C++ Heap Memory Question regarding Delete

    Quick question:

    When I call delete, it returns that object's memory to the heap, right?

    That being true, then when a program exits, then is delete called for every heap allocation made in the program, or does this need to be done manually (via a linked list or just keeping track of the pointers)?

  2. #2
    Join Date
    Jun 2002
    Posts
    224

    Re: C++ Heap Memory Question regarding Delete

    You should call “delete” for every “new” call.
    Code:
    int v = new int;
    //...
    delete v;
    If you allocate an array then you must call “delete[]”. “[]” guarantees the object’s destructor is called for each object in array before actual deletion.

    The memory allocated for application is returned to the system once the application is exited, whether the memory was release with “delete” or not.
    Last edited by zdf; February 25th, 2007 at 03:10 PM.
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ Heap Memory Question regarding Delete

    Quote Originally Posted by nevolved
    after the program exits everything is given back to the heap that was allocated during that program
    Wrong. This is not true for all operating systems.

    Whatever is allocated with "new" must be deallocated with "delete", otherwise the program has a memory leak.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ Heap Memory Question regarding Delete

    Quote Originally Posted by zdf
    The memory allocated for application is returned to the system once the application is exited
    See my reply. This is not true for all operating systems.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2002
    Posts
    224

    Re: C++ Heap Memory Question regarding Delete

    Quote Originally Posted by Paul McKenzie
    See my reply. This is not true for all operating systems.
    Could you please give us an example?
    Last edited by zdf; February 26th, 2007 at 02:26 AM.
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: C++ Heap Memory Question regarding Delete

    Quote Originally Posted by zdf
    Could you please give us an example?
    Example - AmigaOS. There may be more examples.

    The problem is - what if this application is kept running? What if the application being made is an OS (leaking memory)? What if the application is leaking so much that it itself is not able to get the requested allocation?

    Saying the OS will acquire it back, makes it look very-simple-to-ignore-bug. But it is not!

    enigma - what you are assuming happens with applications running with a garbage collector. C#/Java applications fall into that category but C++ does not.

    For more information refer this FAQ - What is the difference between malloc/free and new/delete?.

    Hope it helps.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ Heap Memory Question regarding Delete

    Quote Originally Posted by zdf
    Could you please give us an example?
    Good old MSDOS -- the OS that practically a majority of PC's were running before Windows 95 came along.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured