Calling exit() with dynamic memory

This is a discussion on Calling exit() with dynamic memory within the C Programming forums, part of the General Programming Boards category; Is it wrong to call exit(1) when a failed memory request happens since there may be dynamically allocated memory that ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Calling exit() with dynamic memory

    Is it wrong to call exit(1) when a failed memory request happens since there may be dynamically allocated memory that exists elsewhere in the program that won't get freed?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Well it would be wrong in an interactive program to call exit() just like that - a user would be very annoyed at losing a lot of work.

    As for the freeing memory, read this
    http://www.eskimo.com/~scs/C-faq/q7.24.html
    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.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it wrong to call exit(1) when a failed memory request happens
    No, but it sure isn't portable because the only portable arguments to exit (and operands to return) are 0, EXIT_FAILURE, and EXIT_SUCCESS. 1 doesn't factor in there.

    >since there may be dynamically allocated memory that exists elsewhere in the program that won't get freed?
    That would be a problem, yes. The trick is to make sure you free all memory allocated prior to the error before terminating.
    My best code is written with the delete key.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Let's assume that there is some exit(1) at some time and that arise problem of freeing memory.

    Does that mean that after exiting the program memory will be released after system restart?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    "memory" that a program uses is in RAM which is always released after system restart. Unless it's a really weird machine that uses non-volatile memory for RAM
    If you understand what you're doing, you're not learning anything.

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    In modern multi-threaded operation systems, a process has a memory space with the asm code, a memory heap, initialized variables and command line args (and probably something else). When the process ends (exit() does that) all the memory that refered to that process is freed, therefore releasing the dinamicly allocated memory isn't necessary, because that is done by the OS. You need to free memory IF you don't want to terminate the process, to keep memory consumtion low, and efficiency high.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xErath
    When the process ends (exit() does that) all the memory that refered to that process is freed, therefore releasing the dinamicly allocated memory isn't necessary, because that is done by the OS.
    You don't run Windows, do you?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I don't know about windows, but I do know that linux does free all memory that a program used when it is done. I don't take advantage of that fact though.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  9. #9
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by quzah
    You don't run Windows, do you?

    Quzah.
    Yes I do... It you be annoing to restart my computer every time I play doom 3. And I keep my computer without junk. Try killing some processes once in a while. I usually leave my computer on for 2 weeks in a row. Never had problem, besides a bit over-heat harddrives.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It was a joke. Actually, it was only half joking. XP is pretty good, but Win98, 95, ME, etc, had HORRIBLE memory problems.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by quzah
    It was a joke. Actually, it was only half joking. XP is pretty good, but Win98, 95, ME, etc, had HORRIBLE memory problems.

    Quzah.
    That I fully agree!!!!
    And Win2000 also.

  12. #12
    .
    Join Date
    Nov 2003
    Posts
    307
    Quote Originally Posted by Prelude
    >Is it wrong to call exit(1) when a failed memory request happens
    No, but it sure isn't portable because the only portable arguments to exit (and operands to return) are 0, EXIT_FAILURE, and EXIT_SUCCESS. 1 doesn't factor in there.
    #include <sysexits.h>
    is not standard but it is helpful - at least in the Unix world.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  2. Why use Dynamic Memory
    By appleGuy in forum C++ Programming
    Replies: 11
    Last Post: 08-28-2006, 02:46 PM
  3. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM

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