Thread: Memory Leak

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

    Memory Leak

    I asked my friend a question to see what kind of programmer he is. He's the kind of programmer that just wants it to run, and let the OS take care of any system problems.

    So I asked him what about memory leaks, like if a pointer (in practical use) was to be moved around and all that. He said everything would be destroyed once the program was shut down, so he doesn't spend the time worrying about those things.

    I have been doing C programming and memory leaks weren't that big of a deal since you can't just have a pointer pointing to an object, a variable had to exist, except for using files. In that case, once the file was closed you must set the pointer to NULL.

    I got this book for C++, and I just learned I could set a pointer to an object using new. Then it said if I did not delete the object, and I moved the pointer, there would be a memory leak:
    Code:
    /*
    creates memory leak
    */int *pint=new int(1024);
    int var=241;
    pint=&var;
    So now I'm wondering, would the memory not be cleared once the program is terminated because it's no longer associated with the program? Or will it be fine?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In most cases leaked memory will be freed by the OS when the program finishes. The bigger problem with memory leaks, other than just bad coding style, is that they might accrue over time as your program is running, hogging system resources. In addition, common C++ programming techniques include performing work in object destructors. If the memory for those objects is never freed, then they are never destructed and the code is not run.

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Whoa, not worrying about memory leaks? Thats not a good thing at all. Usually memory is reclaimed by the OS, but not always. I've had accidental leaks which bring the system to a crawl. Its bad form to leak memory, there's no excuse, its being lazy at coding altogether. In something Java or C# there is a garbage collector ( __gc for c# I believe) but in regular c, or c++ there is not.

    In any event, usually it is, but truely not a good thing ever to leak memory as such.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Quote Originally Posted by dpro
    Whoa, not worrying about memory leaks?
    Yeah.... He said it wouldn't be that much anyway. Only what was allocated to hold the value.

    Hmm... bringing the system to a crawl... lol, yup, that's definitely a problem.

    Thanks

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    That and its a lazy way to program... I mean usually its not the end of the world, but it can cause major havoc with the system

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Berticus
    I have been doing C programming and memory leaks weren't that big of a deal since you can't just have a pointer pointing to an object, a variable had to exist, except for using files. In that case, once the file was closed you must set the pointer to NULL.
    You have a bit of a misconception here. Have you never used malloc() and free() in C? They are similar to new and delete.

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