• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: message to nil? (very basic question)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: message to nil? (very basic question)


  • Subject: Re: message to nil? (very basic question)
  • From: Ondra Cada <email@hidden>
  • Date: Mon, 21 Jan 2002 00:20:01 +0100

Vince,

>>>>>> Vince DeMarco (VDM) wrote at Sun, 20 Jan 2002 14:50:58 -0800:
VDM> >AA> >Is it allowed to send a message to nil or will it crash or give
VDM> >some AA> >runtime errors or such things?
VDM> >AA>
VDM> >AA> This is not always safe!!!
VDM> >
VDM> >It *IS* *ALWAYS* *UTTERLY* safe!!!!
VDM> >
VDM> Read the rest of Andrew's message a bit more carefully, he is correct.

I am sorry to disagree with you, but he is not -- I am afraid it's you who
read the thing too quickly. Re-read please the original (quoted) question:
"Is it allowed to send a message to nil or will it crash or give runtime
errors or such things?"

The truth, only truth and whole truth is: "It *is* allowed, (unless there is
a bug in compiler or somebody messed up with the program code or so) it will
*never* crash nor give runtime errors nor such things".

Then, a related but *DIFFERENT* question might occur: "If I send a message
to nil, can I use the return value?"

Now the (almost, see below) proper answer is

VDM> >What might not be safe is interpreting the return value in case it is
VDM> >not convertible from id. You can _send_ a message to nil quite securely
VDM> >regardless the return type, though.

Incidentally, I admit my "convertible from id" could and probably would be
understood in compile-time context, ie. "can be casted". That is not true,
and the fault is mine -- I've used a bad term. It was, of course, meant in
run-time context, ie. "can be directly used".

In other words, so as you could depend on [nil anything] returning "zero",
then the return value
- must be stored (by the compiler) the same way (IIRC, in practice it means in R0);
- the "zero" must be understood as "all bits zeroed" which might or might
not represent a "zero" in the actual target type (with all standard C types
this does not mean a problem; with float there can be no confusion, since it
is excluded by the previous rule anyway).

VDM> If the nil object returns a float or a structure you might not get back
VDM> 0. 0 in the case of floats or and empty stucture.

Neither it (presumably, not tested) would work with long long values, for
the very same reason. OTOH, it *will* work properly with small structures,
which the compiler is able to store inside a word (ie. inside a register) --
unless that behaviour is changed, and new GCC never stores a struct in a
register, whatever small it is.

To sum it all up:

(i) it is *ALWAYS* safe to send any message to nil. No caveats at all.
(ii) it is *SOMETIMES* safe to use the values returned. If it is all right
-- it is described above in which cases and why --, the value is zero (nil,
NULL, NO).
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
2K Development: email@hidden http://www.2kdevelopment.cz
private email@hidden http://www.ocs.cz/oc


References: 
 >Re: message to nil? (very basic question) (From: Vince DeMarco <email@hidden>)

  • Prev by Date: Mysterious threads & ObjectAlloc crashes
  • Next by Date: A slight addendum to the Finder recognizing link/unlink problem
  • Previous by thread: Re: message to nil? (very basic question)
  • Next by thread: Re: message to nil? (very basic question)
  • Index(es):
    • Date
    • Thread