Thread: What can happen if you forget to deallocate memory?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Question What can happen if you forget to deallocate memory?

    Hello everybody! I have a question, What happens if you forget to deallocate memory?
    I ask this because of a line i read here:

    http://www.cprogramming.com/tutorial/lesson6.html

    "After deleting a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer, the pointer becomes a null pointer, in other words, it points to nothing. By doing this, when you do something foolish with the pointer (it happens a lot, even with experienced programmers), you find out immediately instead of later, when you have done considerable damage."

    I know this is probably a very newbie question but I've only been learning C++ for a little less than two weeks.

    What exactly constitutes "Considerable damage"? Just to the program itself? Or my computer self destructing in a hellish fireball of...not....working...?

    Also, If I were to slip up and do something wrong, What can be done to fix this "Considerable damage"?

    Anyways, Thanks to anyone who takes the time to read my question and answer it!

    (Sorry for what I'm sure is a very silly question.)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What exactly constitutes "Considerable damage"?
    Undefined behaviour, such as that arising from double deletion (deleting a null pointer is perfectly fine).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that in C++, we can typically use smart pointers, such as boost::shared_ptr to take care of our pointers for us. No manual delete, no forgetting to set to NULL.
    Unless there's a good reason not to, we should use them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think anything really bad will happen on a modern OS to the computer (unless, perhaps if you do really low-level stuff). Programmers make mistakes and the OS is designed to take that into account.

    It can do considerable damage mainly to your program. (You've been running a simulation for hours, and just as you are about to get the results, a double delete - or memory leak - crashes it. Or it may lead to inexplainable misbehaviour in seemingly unrelated portions of the program, etc.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    forget to deallocate means you get a memory leak. The memory usage of your program will climb, until it uses up all your computer's memory, and then start swapping to disk, and your computer will slow down to a crawl. You can just kill your program, though. No harm will be done to anything except your program, assuming you are using a modern OS.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...And if it manages to gobble up the entire virtual memory, it will crash.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Lightbulb

    I'm running windows vista with codeblocks. So it won't freak out and delete something important or crash my computer, unless i go in there trying to do something crazy and mess up then?

    I'm still going to be careful with it, I just want to make sure my computer doesn't uninstall my OS, explode in a massive fireball, Or open the disk drive and fire a ninja star at mach three into my forehead

    (Seriously though, I'm new at this and just wanted to make sure that if i left out something, it wouldn't brick my computer unless I was trying to mess with something sensitive. Which of course i wouldn't attempt since I'm so new to C++ )
    Last edited by avaldi; 06-16-2008 at 12:25 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by avaldi View Post
    I'm running windows vista with codeblocks. So it won't freak out and delete something important or crash my computer, unless i go in there trying to do something crazy and mess up then?
    Oh no no no, that it won't do. Not from running a program (unless you run in kernel mode or hit a user mode bug, but that's very, very, very unlikely).

    I'm still going to be careful when with it, I just want to make sure my computer doesn't uninstall my OS, explode in a massive fireball, Or open the disk drive and fire a ninja star at mach three into my forehead
    You can feel safe, because it won't

    (Seriously though, I'm new at this and just wanted to make sure that if i left out something, it wouldn't brick my computer unless I was trying to mess with something sensitive. Which of course i wouldn't attempt since I'm so new to C++ )
    Modern operating systems such as Vista don't die from memory leaks. You have nothing to worry about.
    When the program exits, the memory is restored to the OS anyway (see it as a fail safe mechanism).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    ...And if it manages to gobble up the entire virtual memory, it will crash.
    Memory allocation will fail, but I am not sure if it will crash. Even if it doesn't "crash" though, I guess other programs won't work anymore if they can't get any more memory (every new and malloc will fail).

    It's hard to deplete the entier virtual memory, though, considering how slow harddrives are

    I'm running windows vista with codeblocks. So it won't freak out and delete something important or crash my computer, unless i go in there trying to do something crazy and mess up then?
    True. Your computer won't get messed up just by not releasing memory or access out of bound (unallocated memory etc). Just your program will crash.

    I just want to make sure my computer doesn't uninstall my OS, explode in a massive fireball, Or open the disk drive and fire a ninja star at mach three into my forehead
    It won't =).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    Even if it doesn't "crash" though, I guess other programs won't work anymore if they can't get any more memory (every new and malloc will fail).
    No, that's not entirely true.
    The other processes still have their own virtual memory.
    Yes, it would probably end up in the page file, but... they'll still work.

    It's hard to deplete the entier virtual memory, though, considering how slow harddrives are
    Oh ho! No, it isn't. It entirely depends on what you're doing. It's easy to consume the entire virtual memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I'm still going to be careful with it, I just want to make sure my computer doesn't uninstall my OS, explode in a massive fireball, Or open the disk drive and fire a ninja star at mach three into my forehead
    Oh nonono, this only happens if you use void main() or sloppy indentation in your code!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Neo1 View Post
    Oh nonono, this only happens if you use void main() or sloppy indentation in your code!
    I think it comes as a standard feature in Windows Ultra Pro Gold Century Edition with Enterprise Extensions.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Talking

    Alright thanks guys!

    I appreciate all of your help!

    *Takes off ballistic face mask*

    I need no longer fear super sonic ninja weapons!

    (Seriously though, Thanks guys!)

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I realize this is a pointless reply, but I thought I would post for the fun of it.

    I just built a new system. I found a really good setup.

    I have 1 500gb 7,200rpm data drive. Then a 10,000rpm 36.7gb game drive. then a 32gb SSD for my OS. This is an amazing setup in my opinion. the Solid State Drive for the OS is the best. When you have memory swapping it is almost instant due to the solid state design.

    In my opinion SSD is amazing. Pricey yes, but worth every penny for programming or gaming. Swapping Windows Vista from a 5000rpm hard-drive to the SSD made a world of difference.

    I know this is mostly unrelated, but i posted it anyway.

    Get a Solid State Drive if you can afford to spend $500-$750. It is worth it in every since. On my laptop SSD lowered my average system temp from 115 degrees to about 96 degrees.

  15. #15
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No, that's not entirely true.
    The other processes still have their own virtual memory.
    Yes, it would probably end up in the page file, but... they'll still work.
    Depends completely on the computer, OS, and what's in the system at the time.
    What if the page file is full? (or non-existant?) Granted, in Windows, the page file seems to be unbounded in limit (probably a tweak somewhere I don't know, but my pagefile seems to be 4GB at the moment...) Now, my Linux box, the swap will not grow to infinity - there's are hard limit at ~1.5GB, RAM + swap combined. Less if I turn swap off. The man pages state this:
    By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer.
    Of course, regardless of OS, the lesson at the end of the day is that if your program leaks any memory, that's a bug that should be addressed.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM

Tags for this Thread