Thread: new and delete keywords

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    new and delete keywords

    I have a small question. If you use the new keyword but forget to delete, I know that there exists unfreed occupied space in the heap. Does that unfreed space remain that way forever, or is there a way to refree that area of the heap?

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Once your program ends the OS should reclaim it.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Automatically? If that's the case, why is it so important to use delete?

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Because if that new is in a function that gets called often, then you may run out of memory before your application ends. Imagine in a game that is redrawing the screen at say 70 fps. If every frame it new'd some memory but didn't delete it when it was done, your game would come to a halt pretty quickly ( well depending on amount of memory allocated). So basically if you are done with a chunk of memory, delete it.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Ok I get it. So you're saying that you need to delete your memory to keep your program running smoothly? And say that, just as in your example, you forget to delete and the program becomes very slow and the heap memory is overloaded, when you quit the program all the memory is refreed by the OS?

  6. #6

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Got it. Thanks for that.

Popular pages Recent additions subscribe to a feed