2011-04-22 74 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