Like Tree8Likes

Consequences of Memory allocation and de-alocation...

This is a discussion on Consequences of Memory allocation and de-alocation... within the C++ Programming forums, part of the General Programming Boards category; Hello friends..! I just have a doubt in this concept.. I know it is a must to free up memory ...

  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,308
    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
    21,893
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    1,625
    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.
    laserlight likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    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
    Rehman khan likes this.

  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
    21,893
    Quote Originally Posted by Rehman khan
    What should i save...?
    What do you want to save?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,893
    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.
    rags_to_riches and Elysia like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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..

Page 1 of 2 12 LastLast
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, 02:13 PM
  4. Memory Allocation
    By rahulsk1947 in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 12:06 AM
  5. alocation of a matrix
    By spank in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21