Thread: Damn memory Question again

  1. #1
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115

    Angry Damn memory Question again

    I was wondering if anyone can tell me what should a program do if there is a memory allocation error(besides exit the program). If there is other memory allocated should that be freed first? or should the program just terminate where it is?
    Also I was wondering if anyone can tell me why my system doesn't show any memory loss if I dont use free. I ran a test program that basically was like this:
    Code:
    int main(int argv, char *argc[]) 
    { 
       int *ptr = NULL; 
       int i;
    
       for(i=0;i<2;i++)
       { 
          if(!(ptr = malloc(10000000 * sizeof(int)))) 
              printf("Memory error"); 
       } 
       return(0);  
    }
    Now I was running Nortons System doctor at the same time I ran this program. My system was at 68 megs free, when I run the program it drops down to 19 megs free or something like that which is normal for this program. Now when the program finishes, my system reports 68 megs free or so again . What gives? I would think that nortons would show this memory as being tied up. I also used windows xp system tools and it gives me the same results. Now what I think is going on is that the memory is tied up but windows knows that this memory can be used and reports it back as being free. The reasoning behind this conclusion is that I ran this program about 15 times, and after I was done I tried to open windows explorer and it took over a minute to open(I am on a 1 gig with 128 megs of ram so this isn't normal) but my free ram was showing 70 megs or so. What I am thinking took so long was window trying to get back this memory that it told me was free. Am I anywhere near being right about this? or even kinda right(somehow in my twisted logic)
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I think the memory is handed back over to the OS once the program finished executing.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I don't think the mem is released.

    Could be it is not physical mem (RAM), but virtual mem (HD space). Particularly as you are allocing approx 80Mb (more than you have free).

    Yes, if alloc (ect) fails you must release all other mem before exiting.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    This is OS dependant, folks.

    POSIX systems (UNIX/Linux) will reclaim all allocated memory when a program terminates, with the exception of shared memory. I am not certain about Windows, but I believe it behaves similarly.
    Jason Deckard

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    For windows the memory will be freed also......

    The system destroys a process when the last thread of that process has stopped executing.......that means stacks, mappings and heaps.....

    The reason why it took so long to open could be due to defragmenting with the paging file........you had a lot of writing back and forth so it cant do much good.....

  6. #6
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    hmm... makes sense... thanks for the help guys
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 04:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  5. c style string memory leak question
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2003, 06:31 AM