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?
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?
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.
>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.
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?
"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.
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.
You don't run Windows, do you?Originally Posted by xErath
Quzah.
Hope is the first step on the road to disappointment.
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
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.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.
Hope is the first step on the road to disappointment.
That I fully agree!!!!Originally Posted by quzah
And Win2000 also.
#include <sysexits.h>Originally Posted by Prelude
is not standard but it is helpful - at least in the Unix world.