gdb中的nil没有定义为0x0?
我正在使用 gdb(在 Xcode 内部)单步执行一些简单的 Objective-C 代码,并注意到一些奇怪的事情。这是相关的片段:
NSString *s = nil;
int x = (s == nil);
正如我所期望的,这两行之后的 x 的值为 1
。奇怪的是,如果我在 gdb 中尝试类似的东西,它的工作方式并不相同:
(gdb) print ret
$1 = (NSString *) 0x0
(gdb) print (int)(ret==nil)
$2 = 0
(gdb) print nil
$3 = {<text variable, no debug info>} 0x167d18 <nil>
看起来 gdb 对 nil 有一些定义,而不是 Objective-C 使用的(0x0)。有人能解释一下这是怎么回事吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你的代码被编译时,
nil
是一个预处理器常量,定义为__null
(一个特殊的GCC变量,充当NULL
),< code>0L 或0
:那么,gdb 在运行时获取的
nil
从哪里来呢?您可以从 gdb 给出的消息中看出nil
是位于该地址的变量的名称:不出所料,它的值是
0
:这个变量在哪里来自? GDB可以告诉我们:
确实,当我们检查Foundation中定义的符号时,我们发现
nil
:When your code is being compiled,
nil
is a preprocessor constant defined to be either__null
(a special GCC variable that serves asNULL
),0L
, or0
:So, where does the
nil
that gdb picks up at runtime come from? You can tell from the message gdb gives thatnil
is 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
:它指向内存中的地址,而不是变量内容。
It's pointing to the address in memory, not the variable contents.