2011-04-22 51 views
2

示例代码:分析浮点值

int main() 
{ 
     float x = 456.876; 

     printf ("\nx = %f\n", x); 

     return 0; 
} 

在gdb中,我执行这个代码:

Breakpoint 1, main() at sample_float.c:5 
5    float x = 456.876; 
(gdb) n  
7    printf ("\nx = %f\n", x); 
(gdb) p &x 
$1 = (float *) 0x7fffffffd9dc 
(gdb) x/4fb &x 
0x7fffffffd9dc: 33  112  -28  67 

是否有可能看到x的地址的值,使用:X/fb命令为:456.876?

谢谢。

回答

9

也许我误解你的问题,但你可以简单地做

p/f x 

或者

x/f &x 

是不是你想要的?

+1

或者'x/fw&x'如果你真的想更加明确的大小。 – 2011-04-22 12:07:47

+0

非常感谢,这正是我一直在寻找的! – 2011-04-22 18:01:03

1

同意上面的答案,但要理解为什么你得到了你所做的结果。

(gdb) x/4fb &x 
0x7fffffffd9dc: 33  112  -28  67 

从GDB手册

x/3uh 0x54320' is a request to display three halfwords (h) of memory, formatted as unsigned decimal integers (的u'),起始地址为0×54320。

因此,x/4fb & x将一个字节格式化为浮点四次。 不是 4个字节作为浮点数。

+0

感谢您的澄清! – 2011-04-22 18:03:23