Nil still represents an address, yes?


#1

Just a really quick question arising from pg. 69 after the treatment on NULL.

It says : [quote]“Later, when you are learning about pointers to objects, you will use nil instead of NULL. They are equivalent, but Objective-C programmers use nil to mean the address where no object lives”[/quote]

If NULL is zero, I’m assuming nil is zero also. Can I think of it as an address represented by 0?

So if you were to assign an object to nil, you are at least initialising it to a non-address, as in a non-‘working’ address? Is that correct? Albeit without setting it a value?


#2

Yes.

But that’s implementation detail. It is better to think of nil and NULL as denoting unique and distinguished address values, outside the range of address values which the user applications are permitted to mess with.

Thus when you encounter a variable whose value is nil or NULL, you can immediately tell that the variable does not denote an object you are allowed to mess with. Thus, nil and NULL can be conveniently used as markers to signal the end of a sequence of address values in user-address space.

For example:

const char * foo [3 + 1];  // 1 more for the end of array
foo [0] = "f";
foo [1] = "o";
foo [2] = "o";
foo [3] = NULL;  // end of array

const char * p = NULL;
unsigned long x = 0;
while ((p = foo [x++]) != NULL) {
    printf ("%s\n", p);
}

[Become a competent programmer: pretty-function.org]


#3

Thanks ibex, that’s a great help to me. Just couple of things I still don’t understand :confused:

What do you mean exactly by ‘user applications’? I’ve understood users to be things like other objects, etc.

Also, the 0, is it a value or an address? I thought it was a value with an unknown address (as in unknown to users). Is that correct?


#4

[quote]What do you mean exactly by ‘user applications’? I’ve understood users to be things like other objects, etc.
[/quote]
An example of a user application is a program that you create. Another example is a third-party web browser such as Google Chrome. User applications run in a restricted environment.

There are also other kind of applications that belong to the operating system such as Finder and Disk Utility. They run in a less restricted environment.

[quote]Also, the 0, is it a value or an address? I thought it was a value with an unknown address (as in unknown to users). Is that correct?
[/quote]
0 is an abstract value. It can be the value of anything than can be zero. An address is a value too, but a value that denotes the start of a sequence of bytes in your computer’s (virtual) memory.

int fooBar = 0;

int month = 0;

double pressure = 0;

char * address_of_first_byte = 0;

#5

Excellent, thanks a lot re. explanation on 0.

Yep, I know those kinds of user applications. Thought you were using the term to mean something else.

Thanks again!