swarm-support
[Top][All Lists]
Advanced

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

Request for 2 Obj-C explanations. NULL/nil and "empty int" return


From: Paul Johnson
Subject: Request for 2 Obj-C explanations. NULL/nil and "empty int" return
Date: Mon, 28 Jun 1999 13:36:04 -0500

I think these are really a Obj-C questions, but I don't find much to
read about it. I'm not asking for  programming help, but rather for an
explanation that can help me understand these things on a conceptual
level.

1.  NULL versus nil. Sometimes when an object does not exist, example
code says "return nil" and other times it says "return NULL".   I know
that, when a function is supposed to return an object, and the object
does not exist inside the function, then the function returns a nil,
correct?  

But it seems not to matter if I test the return against the value of
NULL or nil.  A test like: 
        if([target getOject]!=NULL) 
"seems" to work fine.  I see plenty of Swarm examples that use NULL. 

But to be perfectly accurate, they ought to use nil.  But the programs
run, and I've wondered why from time to time, and now I think I have an
explanation.  Try this for size.

nil is the value of an id type variable that points to nothing (the
value 0).  I understand the NULL is the value of any kind of pointer
that  points to nothing (0).  It seems logical that setting a variable
of type id to nil or setting it to NULL will have the same effect: make
it point to 0. This is so because an id is itself a pointer, then
setting it equal to NULL works.   When you have set such an id to NULL,
then it will say it is equal to either NULL or nil when you do a
comparison.  Hence, when you do a test on that variable, you get the
same thing from "if ([object==NULL])" as "if ([object==nil])".     

On the other hand, you if you have a pointer to another kind of thing,
not an object, then it can be set only to NULL, but not nil.  The
compiler shouldn't allow it since nil is reserved for id.  
       

2. If a function returns an int, and the integer is not defined within
the function, what is returned then?  Not NULL, I know that.  How do you
test for "undefined integer"?

This came up in the use of command line arguments.  I've set up the
argument parser to take the number of agents from the command line.  In
the ModelSwarm.m, this works fine as long as I remember to put the
option on the command line:


+ createBegin: aZone
{
  ModelSwarm *obj;
  id <ProbeMap> probeMap;
  obj = [super createBegin: aZone];

   obj->numPPL=[arguments getNumpplArg];
 [...blah blah blah]
}

If I forget to put the argument in the command line, then something
interesting happens.  The return from [arguments getNumpplArg] is the
number "13", and I don't quite understand why.  Here is the
MyArguments.m code:

#import "MyArguments.h"
#import <stdlib.h>

@implementation MyArguments

+ createBegin: aZone
 {
   static struct argp_option options[] = {
     {"numppl", 'n', "N", 0, "Set numppl", 3},
     { 0 }
   };
   
   MyArguments *obj = [super createBegin: aZone];

   [obj addOptions: options];
   return obj;
 }

- (int)parseKey: (int) key arg: (const char*) arg
 {
   if (key == 'n')
     {
       numpplArg = arg;
       return 0;
     }
   else
     return [super parseKey: key arg: arg];
 }

- (int) getNumpplArg
 {
    int num;
    if(numpplArg)
      atoi(numpplArg);
    return num;
 }

@end

In the "getNumpplArg" method, num is undefined if no argument is passed
from the command line. I understand how to fix this, that's not really
my reason for writing. I want to know how to understand what is getting
returned when num is undefined in this code and how to test returns to
see if they are undefined.  
 


-- 
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]