Objective-C pointers as booleans

Go To StackoverFlow.com

1

Going through some documentation on modifying CGImageRef data, I came across a strange example -- It went something along the lines of this pseudocode:

void *data = Allocate space for data;
if (data != NULL) Manipulate data;
if (data) Free data;

This got me wondering! What is the difference between the boolean operation if (data != NULL) and the boolean operation if (data).

To be more specific, how do pointers behave in Objective C when they are treated as booleans? Attempting to google this, I only found myriads of questions relating to pointers-to-booleans, as opposed to pointers being evaluated as booleans.

2012-04-04 06:13
by Georges Oates Larsen


5

They're exactly the same. Non-zero values of any type are interpreted as "true" in C, and by extension in Objective-C. C doesn't even have a boolean type.

2012-04-04 06:17
by Carl Norum
Ooh and because NULL can be seen as a pointer to the position 0, it gets evaluated as false... Gotcha, thankyou! : - Georges Oates Larsen 2012-04-04 06:18
Please, don't think of NULL as anything remotely like "a pointer to the position 0". It's simply the agreed value for pointers that don't point to anything. When forced to be a boolean, it equates to false. And when you force a zero to a pointer, you get NULL - David Schwartz 2012-04-04 06:21
@GeorgesOatesLarsen: a null pointer isn't necessarily a pointer to memory location 0. According to the C spec, a null pointer is guaranteed to compare unequal to any valid pointer. On some platforms, memory location 0 may be valid (for example, it may be the first entry in a table of interrupt handlers). A null pointer, however, converts to and from integer value 0, whence a null pointer is interpreted as a false value. See C99, § 6.3.2.3 3 - outis 2012-04-04 06:23
In fact >=C99 has boolean type defined in stdbool.hjacekmigacz 2012-04-04 06:26
@DavidSchwartz I didn't know that NULL could be internally stored as nonzero! Nevertheless, I find that due to all of these automatic conversions, NULL can still be thought of as pointing to the position 0. It's just a simple way of thinking about it, and has no real effect on the process of programming - Georges Oates Larsen 2012-04-04 06:32
@GeorgesOatesLarsen: it can have affects. Though they may be rare, the simplistic view of null pointers can cause bugs. For example, you can access a pointer value directly (which, admittedly, you generally shouldn't do), and your code wouldn't then handle non-null 0 pointers or (worse still) null pointers properly. Better to think of null pointers as having special null values (the concept of "null" and nullable types is useful beyond pointers), like NULL in SQL, None in Python and Nothing in Haskell. Programming is a subtle discipline - outis 2012-04-04 06:43
@outis Ah, I see. Generally, when doing pointer arithmetic, I check for null by using NULL, as opposed to checking for 0. Whether or not I see NULL as zero, it is ALWAYS better practice to use the NAME of what you are referring to. Whenever programming with pointers, I always see them as magic numbers, and never worry about their value (other than equality amongst variables). The idea of seeing null as 0 is just an attempt to alleviate my hate for anonymity. Programming is clearly an idiosyncratic practice, so shall we agree to disagree - Georges Oates Larsen 2012-04-04 18:00
Ads