在以下 C 代碼中:
#include <stdio.h>
int main(void)
{
unsigned i = 1;
while (i)
{
i <<= 1;
}
return 0;
}
我首先使用除錯資訊進行編譯,如下所示:gcc -g single-step.c
然后我做lldb a.out. 我想看看我是干什么的,所以我b main和run,之后,我把我的觀察點的我:watchpoint set variable i。然而,當我這樣做時,lldb 似乎為變數 i 分配了一個值,它不應該
Watchpoint created: Watchpoint 1: addr = 0x16fdff498 size = 4 state = enabled type = w declare @ '/Users/d1ff1cult/Documents/KUL 2021-2022/IW/oefenzitting-c/les8-debugging/examples/single-step.c:5' watchpoint spec = 'i' new value: 53472
給 ia 提供看似完全隨機的值,那么我做錯了什么?
我使用 CLion 撰寫代碼,但這發生在 CLion 終端以及我的 macOS 正確終端中。我也在使用 M1 Mac。
提前致謝!
uj5u.com熱心網友回復:
尤金是對的。
在我的系統上,當我運行你的程式時,我在觀察點得到一個初始值 0。
我改變了程式,把你的代碼放在一個函式中(例如)orig。
我創建了一個函式fill,該函式在填充其堆疊幀時回圈填充長度為 100 的 int 陣列。
從main,我打電話fill,然后orig與b orig。然后,執行 watchpoint 命令,我得到的值為 100。
因此,您的觀察點正在顯示分配給的堆疊幀中發生的任何內容i。即未初始化的值。
這是我創建的測驗程式:
#include <stdio.h>
#include <stdlib.h>
int
orig(void)
{
unsigned i = 1;
while (i) {
i <<= 1;
}
return 0;
}
void
fill(void)
{
unsigned x[100];
for (int i = 0; i < 100; i)
x[i] = rand();
}
int
main(void)
{
fill();
orig();
return 0;
}
這是除錯輸出:
> lldb ./fix1
(lldb) target create "./fix1"
Current executable set to './fix1' (x86_64).
(lldb) b main
Breakpoint 1: where = fix1`main 4 at fix1.c:29, address = 0x000000000040117b
(lldb) b orig
Breakpoint 2: where = fix1`orig 4 at fix1.c:7, address = 0x000000000040112a
(lldb) run
Process 193289 launched: '/tmp/watch/fix1' (x86_64)
Process 193289 stopped
* thread #1, name = 'fix1', stop reason = breakpoint 1.1
frame #0: 0x000000000040117b fix1`main at fix1.c:29
26 main(void)
27 {
28
-> 29 fill();
30 orig();
31
32 return 0;
(lldb) c
Process 193289 resuming
Process 193289 stopped
* thread #1, name = 'fix1', stop reason = breakpoint 2.1
frame #0: 0x000000000040112a fix1`orig at fix1.c:7
4 int
5 orig(void)
6 {
-> 7 unsigned i = 1;
8
9 while (i) {
10 i <<= 1;
(lldb) watchpoint set variable i
Watchpoint created: Watchpoint 1: addr = 0x7fffffffdd6c size = 4 state = enabled type = w
declare @ '/tmp/watch/fix1.c:7'
watchpoint spec = 'i'
new value: 100
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/378581.html
上一篇:有沒有辦法快速更新列公式?
下一篇:Excel:作業表中間的滾動條?
