swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: what's wrong with this use of the index to clean a list?


From: Paul Johnson
Subject: Re: what's wrong with this use of the index to clean a list?
Date: Fri, 29 Oct 1999 10:06:46 -0500

Yes, your example is OK because you don't have my mistake in it :)

Suppose you redo your for loop like this:
  for (idx=0;idx<30;idx++)
      {
          coal=[self createObject];
          [aList addLast: coal];
       }

And then you write this method createObject which, for some ACCIDENTAL
reason, does not always return a coal object, but sometimes it comes up
nil.  Perhaps that method has something conditional that you don't
expect will affect the calculation, but it accidentally does, and so
inside that method the object is not created and then coal is nil.  Or
perhaps you are a C user and you put an old fashioned malloc in your
method and you forget to check to make sure the memory is actually
allocated.  So the return sets coal equal to nil.  The Swarm list will
then add a nil to the list.  I had a hard time understanding what it
means to have "nil" in a list, at first.  To a student with that
question, I would say "recall a variable of type id is just a pointer to
an object, and in that case coal is a pointer to a "missing value" (to
use the social science statistical package analogy).  Just as you can
add a pointer to an object in a list, you can add a pointer to a nothing
as well."  I think that explains it.  

That's how a nil can be added to your list. That is how it happened to
me.  And Lots of things were screwed up because of it!

If there is a danger of adding pointers to nil, I think it is
appropriate to check coal objects to make sure they are not nil before
you add them to your list.

Incidentally, in this list we debated a while ago about the difference
between nil and NULL.  Let me see if I understand our conclusion
correctly. On most architectures at the current time, NULL and nil are
be interchangable, but (I think it was Marcus who said) there is no
reason in the future they always will be. So code should use nil to
check for missing Objective-C objects, not NULL, and you should use NULL
to refer to C array values or C pointers.  So if with a swarm list that
is full of id type object, the usual thing that we all do like you do:
        while ([listIndex next]!=NULL)
probably would be better as: 
        while ([listIndex next]!=nil)
Both nil and NULL are macros defined in the libraries on various
systems.

pj
address@hidden wrote:
 
> I tried the following code....
> 

> Do I have the magic touch or what's behind?
> Fabio.
> 
-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]