Thread: Consequences of Memory allocation and de-alocation...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30

    Post Consequences of Memory allocation and de-alocation...

    Hello friends..! I just have a doubt in this concept..
    I know it is a must to free up memory at the end, when it is allocated in the beginning of any program.. Otherwise, it would become un-usable junk..
    Now, my question is what will be the consequence of this code:

    Code:
    #include <iostream>
    using namespace std;
    
    
    class xxx
    {
        long long a[1000],b[1000],c[1000],d[1000],e[1000];
    };
    
    
    int main()
    {
        long long d=0;
        xxx *a = new xxx[10000];
        xxx *b = new xxx[10000];
        
        //   delete []a;
        //   delete []b;
        
        return 0;
    }
    This program uses the allocated memory and if i failed to delete those memory, totally 800,000,000 bytes would become waste...

    Will this kind of code much harmful to pc..??
    How can i recover those wasted memory...??
    Please help me in these issues...!!!!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    1. It depends on your OS. Most of them know which program allocated it and so the allocation is ditched when the program is exited.
    2. If your OS is not one of those, then a reboot should fix it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    So, when that program is executed, it allocates the memory and when that execution is over, OS will clear it automatically...

    Is that what you mean..???

    If it depends on OS, then what's on WINDOWS...? windows 7,xp etc..???

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rehman khan
    If it depends on OS, then what's on WINDOWS...? windows 7,xp etc..???
    Windows (at least the versions still supported) will perform the cleanup after the process has terminated. Still, you should not rely on that in your code.
    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

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Quote Originally Posted by laserlight View Post
    Windows (at least the versions still supported) will perform the cleanup after the process has terminated. Still, you should not rely on that in your code.
    So, even if we didn't use the command 'delete', OS will free up the allocated memory.. fine..

    Now, can you tell me what are the ways to make that allocated memory to be permanent...?
    I mean, it should not be erased at the end of the execution. Is it possible.???

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Why would you want to do that?
    Devoted my life to programming...

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Rehman khan View Post
    Now, can you tell me what are the ways to make that allocated memory to be permanent...?
    I mean, it should not be erased at the end of the execution. Is it possible.???
    Save it to a file.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Whilst the OS will definitely reclaim the memory, it's important to remember that the destructors will NOT be called if you do not call delete.

    So if your class allocated other resources (files, semaphores, device handles and so forth), then things could be less clear cut.

    Cleaning up when you're done (as a habit) means you're more likely to do it when it matters most.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Now, can you tell me what are the ways to make that allocated memory to be permanent...?
    I'm terrified by what this implies; I remember bad "TSR" programs.

    Soma

  10. #10
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Quote Originally Posted by GReaper View Post
    Why would you want to do that?
    Actually, i don't want to do such a thing...!!!Learning is not a sin, right..??? B)

  11. #11
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Quote Originally Posted by oogabooga View Post
    Save it to a file.
    It's better for me to understand, if your answer is more specific..

    What should i save...???

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rehman khan
    What should i save...?
    What do you want to save?
    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

  13. #13
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Quote Originally Posted by Salem View Post
    Whilst the OS will definitely reclaim the memory, it's important to remember that the destructors will NOT be called if you do not call delete.

    So if your class allocated other resources (files, semaphores, device handles and so forth), then things could be less clear cut.

    Cleaning up.....
    Well, first thanks for your response...!!!
    But, you see, i'm not a PRO in c++... So, can you make me understand what you told...???? Here, where is the destructor came from....???
    and what's the relation between destructor and delete as you said...???

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rehman khan
    But, you see, i'm not a PRO in c++... So, can you make me understand what you told...???? Here, where is the destructor came from....???
    and what's the relation between destructor and delete as you said...???
    I think you should work through some introductory material on C++. For example, Accelerated C++ by Koenig and Moo. Comprehensive structured learning is not a sin.
    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

  15. #15
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Quote Originally Posted by laserlight View Post
    I think you should work through some introductory material on C++.............
    Thank you..!! But, that's the book, i'm using for the past 3 months..!!!!

    All I have is, only one confusion.!!! If i didn't use 'delete' OS itself will clean the allocated memory. Fine..!! Then how can i make that allocated memory to be permanent...???

    Please i need answer for this question..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation
    By lars-erik in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2011, 08:24 AM
  2. Memory allocation
    By sarathius in forum C Programming
    Replies: 5
    Last Post: 03-13-2008, 06:21 AM
  3. memory allocation
    By mbooka in forum C Programming
    Replies: 3
    Last Post: 02-28-2006, 03:13 PM
  4. Memory Allocation
    By rahulsk1947 in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 01:06 AM
  5. alocation of a matrix
    By spank in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 12:28 PM