Roopa said:
There was some discussion here on checking for NULL before calling
'free'like
if (ptr != NULL)
free (ptr);
[...]
Now, my views would be since C89/C99 say free is NULL-safe so why
bother about the underlying environment. Am I correct here, or
should we bother to check the documentation/API ref manual of the
underlying OS and then code accordingly.
In my opinion, you're correct; implementations of free() that don't
handle null pointers properly are rare enough that probably it's not
worth the time to worry about them.
free(NULL) is a no-op, and is basically a waste of time. If your
program design has you freeing null pointers, you might consider
cleaning it up a bit, but I wouldn't make it a high priority. If your
program occasionally frees null pointers *accidentally* rather than by
design, that's a bug (and you'd be better off if free(NULL) aborted
rather than doing nothing).
I'm trained well in writing functions who are tolerant to errors.
A NULL pointer will be dedected and handled but anyway as a function
is
1. initialision
can produce NULL pointers
will break down to cleanup the function when NULL pointer is
critical
2. do work
when needed with respect to NULL pointer(s) in detail
3. cleanup. No matter if work was done or not
results in free(NULL) often because a test for is superflous. Makes
the flow more clear.
Having said that, until it died a while ago I had a Sparcstation 2
running SunOS 4.1.3. I had installed gcc on it, so I had a
(basically) C90-compliant compiler, but it still used the old SunOS
runtime. I *think* it misbehaved on free(NULL). For some purposes,
such systems might be worth worrying about.
I've never seen such runtime since more than 16 years. Maybe there are
some pre-ANSI compilers exist who use a runtime where free (NULL) was
an error. But I've never seen such even in that time.
At least the standard is nothing more than a collection of practical
behavior of compilers, unifying them. So commonly used extensions
today will be found in the standard tomorrow. But free(NULL) is even
pre-ANSI practise, so it founds its place in the very first one.
You should know that Twitsdale is propagating "ignore the standard
because its useless" as he is using only one OS and one compiler on it
and had never the need to use another one. The normal developer will
be happy to be trained in the standard because he comes here with open
mind and knowth about the worth writing his code so compatible as
possible.
Even in an application that is designed to be properitary on an
specific OS and compiler there is enough that can be written easy to
be compatible to any OS and compiler on the world. Time changes, OSes
changes, compilers changes. And at that point you're happy about each
finction that is designed to be compatible because you've nothing more
to do than to recompile.
Rewiting things needed to be properiatry is hard enough, having
anything possible compatible to the standard makes porting more easy.
I had to work on a port from one properitary application system to
another OS - and was happy that it was designed to be portable so far
as possible. About 1 million lines of code - and 60% of them written
to be standard compilant.
No chance for the other 40% in any way as this was needed to use
properitary APIs because of performance. But the designers were awake
of the possibility to expand the user base to other OSes and had
encapsulated that.
--
Tschau/Bye
Herbert
Visit
http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!