I was stepping through some simple Objective-C code with gdb (inside of Xcode) and noticed something strange. Here is the relevant snippet:
NSString *s = nil;
int x = (s == nil);
As I’d expect, the value of x after these two lines is 1. Strangely, if I try something similar in gdb, it doesn’t work the same:
(gdb) print ret
$1 = (NSString *) 0x0
(gdb) print (int)(ret==nil)
$2 = 0
(gdb) print nil
$3 = {<text variable, no debug info>} 0x167d18 <nil>
It seems like gdb has some definition for nil other than what objective-C uses (0x0). Can someone explain what’s going on here?
When your code is being compiled,
nilis a preprocessor constant defined to be either__null(a special GCC variable that serves asNULL),0L, or0:So, where does the
nilthat gdb picks up at runtime come from? You can tell from the message gdb gives thatnilis the name of a variable located at that address:Its value, unsurprisingly, turns out to be
0:Where does this variable come from? GDB can tell us:
Indeed, when we check the symbols defined in Foundation, we find
nil: