J
John Eskie
Lately I've seen alot of C and C++ code (not my own) which doesn't do any
checking if memory obtained by new or malloc is valid or if they return NULL
pointers.
Why does most people not care about doing this kind of error checking?
When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
I still do that always but sometimes it's annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.
Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:
blah *p = NULL;
while (!p)
{
p = malloc(...);
}
On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers. Actually I prefer NULL pointers because it's so much
easier to handle in a while loop then doing some exception handling.
I hope I've reached my point now because OS's do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess
what, it crashes in no-time.
In my opinion it's still better to run a while() loop and stall the program
then having it crash instead, no?
I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.
Thanks in advance.
-- John
checking if memory obtained by new or malloc is valid or if they return NULL
pointers.
Why does most people not care about doing this kind of error checking?
When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
I still do that always but sometimes it's annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.
Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:
blah *p = NULL;
while (!p)
{
p = malloc(...);
}
On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers. Actually I prefer NULL pointers because it's so much
easier to handle in a while loop then doing some exception handling.
I hope I've reached my point now because OS's do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess
what, it crashes in no-time.
In my opinion it's still better to run a while() loop and stall the program
then having it crash instead, no?
I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.
Thanks in advance.
-- John